微信小程序分页功能实现概述
本示例提供了微信小程序实现分页功能的详细代码,供读者参考使用。
近期接到一个在小程序中实现分页的需求,由于搜索资源有限且未找到现成Demo,便自行梳理逻辑并编写了代码。目前思路较为直接,后续仍有优化空间,欢迎有更好方案的开发者交流分享。以下是具体实现内容。
小程序端开发部分主要涉及视图层、逻辑层及样式层的配合。
页面结构文件用于定义分页组件的布局。
订单类型:{{item.type}}
>
订单日期:{{item.yvtime}}
订单状态:
{{item.sta}}
{{item.sta}}
上一页
第{{page1}}页
第{{page2}}页
第{{page3}}页
第{{page4}}页
下一页
共{{allpage}}页 当前为第{{nowpage}}页
JavaScript逻辑层负责管理分页状态及发起网络请求。
data: {
allpage:16,//总页数
nowpage:1,//当前页数
page1:1,//第一页
page2:2,//第二页
page3:3,//‘'‘'
page4:4,
step:4,//步长
},
/**主要函数*/
//初始化渲染数据
onLoad: function (options) {
var that = this
wx.request({
url: "你的网址",
data: {
userphone: 你的参数,
},
success: function (res) {
console.log(res);
if (res.data.code == 0) {
that.setData({
allworkflow: res.data.data,//初始数据列表
allpage:res.data.count//数据总页数
})
} else {
wx.showToast({
title: '暂无待处理工作流!',
icon: 'none',
duration: 20000
})
}
}
})
},
/**
* 上一页
*/
pu:function(){
var now = this.data.page1 - this.data.step;
if(now>0){
this.setData({
page1: this.data.page1 - this.data.step,
page2: this.data.page2 - this.data.step,
page3: this.data.page3 - this.data.step,
page4: this.data.page4 - this.data.step,
});
}else{
wx.showToast({
title: '已为第一页',
icon: 'none',
duration: 1000
})
}
},
/**
* 下一页
*/
pd:function(){
if (this.data.page4 < this.data.allpage) {
this.setData({
page1: this.data.page1 + this.data.step,
page2: this.data.page2 + this.data.step,
page3: this.data.page3 + this.data.step,
page4: this.data.page4 + this.data.step,
});
} else {
wx.showToast({
title: '已为最后一页',
icon: 'none',
duration: 1000
})
}
},
/**
* 点击后页面渲染,重新查询数据页面重新渲染
*/
dd_status:function(e){
this.setData({
nowpage: e.currentTarget.dataset.id,
});
var that = this
wx.request({
url: "你的地址",
data: {
userphone: 你的查询参数,
page: e.currentTarget.dataset.id//当前页数的参
},
success: function (res) {
if (res.data.code == 0) {
that.setData({
allworkflow: res.data.data,
})
} else {
wx.showToast({
title: '没有数据的提示!',
icon: 'none',
duration: 20000
})
}
}
})
}
WXSS样式文件用于美化分页控件及列表项外观。
.nav{
background-color: #fff;
padding: 26rpx 0;
color: #7b7b7b;
}
.nav>text{
width: 16.4%;
text-align: center;
display: inline-block;
}
.outer_container{
width:80%;
margin:0 auto;
height:200rpx;
background-color: white;
padding-left:40rpx;
padding-right: 40rpx;
border-bottom:1rpx solid rgb(214, 214, 214);
color: rgb(79, 193, 229);
font-size: 24rpx;
}
.one{
height:100rpx;
line-height: 100rpx;
border-bottom:1rpx solid rgb(218,218,218);
}
.two{
height:100rpx;
line-height: 100rpx;
color:rgb(102, 102, 102)
}
.right{
float:right;
font-size: 46rpx;
line-height: 50rpx;
color:rgb(218, 218, 218);
}
.right_2{
float:right;
line-height: 100rpx;
color:rgb(102, 102, 102);
}
.divLine{
background: #D4DADD;
width: 100%;
height: 4rpx;
}
.right{
width:25rpx;
height:25rpx;
margin-top:20rpx;
margin-right:20rpx;
position:relative;
}
后端服务建议使用PHP编写(也可选用Java等其他语言),主要包含两个接口:一是初次加载时返回总页数等基础信息,二是根据页码参数返回对应页的数据。
/**数量查询*/
$querysum = "select count(id) as sums from yv_order where user_bankid='$user_bankid' order by id desc";
/**数据查询*/
$query2 = "select yvtype,yvtime,sta,id from yv_order where user_bankid='$user_bankid' order by id desc limit 4";
$data=array(
'code'=>0,
'msg'=>'ok',
'data'=>$allorder,
'count'=>ceil($countsum/4),
);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
exit;
当用户点击切换页码时,后端接收页码参数并执行相应的数据查询逻辑。
//之后查询的页面
$userphone=$_GET['userphone'];//你许哟啊查询条件的参数
$page=$_GET['page'];//页数
//我的分页是4条一页,你自己按照你自己的来我只提供思路
if($pagel>0){
$query2 = "select yvtype,yvtime,sta,id from yv_order where user_bankid='$user_bankid' order by id desc limit $pagel,4";
}else{
$query2 = "select yvtype,yvtime,sta,id from yv_order where user_bankid='$user_bankid' order by id desc limit 4";
}
//对不起我只能为你提供片段
$data=array(
'code'=>0,
'msg'=>'ok',
'data'=>$allorder,
);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
exit;
最终界面将展示带有分页控件的列表数据。

以上内容为本次分享的完整代码与思路,希望能对正在学习或开发相关功能的朋友有所帮助,也欢迎大家继续支持本站。