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 协议》,转载必须注明作者和本文链接
关于 LearnKu
这是mpvue吗
感谢您提供的思路与代码 以下是我编写的 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" }