uniapp 小程序列表页上滑加载更多配合 Laravel 分页

<template>
    <view>

        <view v-for="(list, index) in lists" :key="index">
            <view v-for="(item, index2) in list" :key="index2" @tap="func" :data-id="item.id">

            </view>
        </view>

    </view>
</template>

<script>
    export default {
        data() {
            return {
                currentPage:1,
                hasMore:true,
                lists:[],
            }
        },
        onShow() {
            this.queryList();
        },
        methods: {
            onReachBottom:function(){
                if(!this.hasMore){uni.showToast({icon:'none',title: '没有更多了',duration:2000});return;}
                this.queryList(true);
            },
            queryList: function(loadMore=false)
                this.hasMore = true;
                let page = this.currentPage;
                if(loadMore){ page = ++this.currentPage }else{ page = 1; this.currentPage =1;}

                this.$http.get('',{params:{page:page}}).then(res=>{
                    let data = res.data.data;
                    if(data.pageinfo.total == 0){this.lists = [];return;}
                    if(!loadMore){this.lists = []}
                    this.lists.splice(page-1,0,data.list);
                    this.currentPage = data.pageinfo.current_page;
                    if(Number(data.pageinfo.current_page) >= Number(data.pageinfo.last_page)){
                        this.hasMore = false;
                    }
                }).catch(err=>{

                })
            },
            func : function(e){
                let id = e.currentTarget.dataset.id;
            }
        }
    }
</script>

<style></style>

laravel

public function list(){

        $records = List::where('is_del',0)->orderBy('id','desc')->paginate(10);

        $list = $records->toArray()['data'];

        return Y::json([
            'list'     => $list,
            'pageinfo' => [
                'total'        => $records->total(),
                'per_page'     => $records->perPage(),
                'current_page' => $records->currentPage(),
                'last_page'    => $records->lastPage(),
            ]
        ]);
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
welcome come back
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3
北冥

这是mpvue吗

3年前 评论
php_yt (楼主) 3年前

感谢您提供的思路与代码 以下是我编写的 UNIAPP VUE3 TS 版本

let currentPage = 1
let hasMore = ref(true)
let lists = ref([])

onReachBottom(async () => {
    if (!hasMore) {
        uni.showToast({ icon: 'none', title: '没有更多了', duration: 2000 })
        return;
    }
    queryList(true);
})
async function queryList(loadMore = false) {
    hasMore.value = true;
    status.value= "loading"
    let page = currentPage;
    if (loadMore) {
        page = ++currentPage
    } else {
        page = 1
        currentPage = 1
    }

    //访问获取数据
    const res: any = await LTRequestApi.Hometest({page:page})
    let data = res.data;
    if (data.pageinfo.total == 0) { 
        lists = []  
        return 
    }
    if (!loadMore) { lists = [] }
    lists.splice(page - 1, 0, data.list);
    currentPage = data.pageinfo.current_page;
    if (Number(data.pageinfo.current_page) >= Number(data.pageinfo.last_page)) {
        hasMore.value = false
        status.value="nomore"
        return
    }
    status.value="loadmore"
}
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
95
粉丝
24
喜欢
156
收藏
347
排名:323
访问:2.9 万
私信
所有博文
社区赞助商