小程序怎样实现列表分页展示及上下页点击
Admin 2022-11-19 群英技术资讯 1536 次浏览
这篇文章主要讲解了“小程序怎样实现列表分页展示及上下页点击”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“小程序怎样实现列表分页展示及上下页点击”吧!具体内容如下
主要实现功能:
1.列表展示
2.上下页点击
效果图:

知识点:wx:for、bindtap、生命周期函数–监听页面加载、.filter、取余( % )取整(parseInt(x/y) )函数
js
data: {
frontPage: false,//上一页 存在true,不存在false
nextPage: false,//下一页 存在true,不存在false
pages: 0,//所有页
thisPages: 0,//当前页
rows: 6,//每页条数
total: 0,//总条数
pageData: [],//本页显示的列表数据
prizeListItem:[
{name: "张三", pic: "../../images/1.png", gift:"小蛋糕"},
{name: "李四", pic: "../../images/2.png", gift:"冰淇淋"},
{name: "陈工", pic: "../../images/3.png", gift:"按摩椅"},
{name: "孙悟空", pic: "../../images/3.png", gift:"桃子"},
{name: "猪八戒", pic: "../../images/2.png", gift:"红烧肉"},
{name: "萨赫尚", pic: "../../images/1.png", gift:"新衣服"},
{name: "程序员", pic: "../../images/2.png", gift:"电脑"},
{name: "甄姬", pic: "../../images/3.png", gift:"口红"},
{name: "孙悟空", pic: "../../images/3.png", gift:"桃子"},
{name: "猪八戒", pic: "../../images/2.png", gift:"红烧肉"},
{name: "萨赫尚", pic: "../../images/1.png", gift:"新衣服"},
{name: "程序员", pic: "../../images/1.png", gift:"电脑"},
{name: "甄姬", pic: "../../images/2.png", gift:"口红"}
],
myPrize: false,
tab1: '',
tab2: 'selected',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function () {
this.setList();
},
// 初始化列表分页
setList() {
let that = this;
let thisPages = that.data.thisPages;
let rows = that.data.rows;
let prizeListItem = that.data.prizeListItem;
let pageData = that.data.pageData;
let pages = that.data.pages;
if (pageData !== []){
pageData = prizeListItem.filter(function (item, index, prizeListItem) {
//元素值,元素的索引,原数组。
return index >= rows*thisPages && index <= rows*(thisPages+1)-1; //初始为0,0 < index < 6-1
});
let x = 0;
let y = prizeListItem.length;
if ( y%rows !== 0){
x = 1
};
pages = parseInt(y/rows) + x; //所有页
thisPages = thisPages +1; //当前页
if ( pages > 1){
that.setData({
nextPage: true,
})
}
that.setData({
thisPages: thisPages,
pageData: pageData,
pages: pages,
rows: rows,
})
}
},
//点击下一页
clickNext() {
let that = this;
let thisPages = that.data.thisPages;
let prizeListItem = that.data.prizeListItem;
let pageData = that.data.pageData;
let pages = that.data.pages;
let rows = that.data.rows;
pageData = prizeListItem.filter(function (item, index, prizeListItem) {
//元素值,元素的索引,原数组。
return index >= rows*thisPages && index <= rows*(thisPages+1)-1;
});
thisPages = thisPages + 1;
if ( pages === thisPages){
that.setData({
nextPage: false,
})
}
that.setData({
thisPages: thisPages,
pageData: pageData,
frontPage: true,
})
},
//点击上一页
clickFront() {
let that = this;
let thisPages = that.data.thisPages;
let prizeListItem = that.data.prizeListItem;
let pageData = that.data.pageData;
let rows = that.data.rows;
pageData = prizeListItem.filter(function (item, index, prizeListItem) {
//元素值,元素的索引,原数组。
return index >= rows*(thisPages-2) && index <= rows*(thisPages-1)-1;
});
thisPages = thisPages - 1;
if ( thisPages === 1){
that.setData({
frontPage: false,
})
}
that.setData({
thisPages: thisPages,
pageData: pageData,
nextPage: true,
})
},
wxml
<view class="prizelist">
<view class="info_con">
<view class="list" wx:for="{{pageData}}">
<image class="list_bg" src="../../images/wi_listbg.png"></image>
<view class="list_head">
<image class="list_headpic" src="{{item.pic}}"></image>
<view class="list_name">{{item.name}}</view>
</view>
<view class="list_prize">{{item.gift}}</view>
</view>
</view>
<view class="paging">
<view class="page_btn">
<view wx:if="{{frontPage}}" bindtap="clickFront">上一页</view>
</view>
<view class="page_num">第{{thisPages}}页 共{{pages}}页</view>
<view class="page_btn">
<view wx:if="{{nextPage}}" bindtap="clickNext">下一页</view>
</view>
</view>
</view>
wxss
【外框
.con .prizelist{
width: 100%;
height: max-content;
margin-top: 38rpx;
}
.con .prizelist .info_con{
width: 639rpx;
height: 787rpx;
display: inline-block;
}
【list的样式
.con .prizelist .info_con .list{
width: 639rpx;
height: 108rpx;
position: relative;
margin: 20rpx 0;
}
.list .wi_prize{
width: 186rpx;
height: 69rpx;
margin: 20rpx 0 0 60rpx;
}
.list .prizeinfo{
width: 350rpx;
height: 108rpx;
float: right;
}
.list .prizeinfo .prize_name{
font-size: 28rpx;
color: #87562e;
line-height: 42rpx;
margin-top: 20rpx;
}
.list .prizeinfo .prize_state{
font-size: 20rpx;
color: #ff2d2d;
line-height: 25rpx;
}
.list .list_bg{
width: 639rpx;
height: 108rpx;
position: absolute;
left: 56rpx;
z-index: -1;
}
.list .list_head{
height: 100%;
width: max-content;
margin-left: 100rpx;
float: left;
}
.list .list_head .list_headpic{
border-radius: 50%;
background-color: rgb(43, 93, 216);
width: 46rpx;
height: 46rpx;
margin: 31rpx 0rpx;
float: left;
}
.list .list_head .list_name{
color: #000;
line-height: 108rpx;
font-size: 28rpx;
float: left;
margin-left: 31rpx;
}
.list .list_prize{
height: 100%;
line-height: 108rpx;
font-size: 28rpx;
color: #87562e;
float: right;
}
【上下页样式
.con .prizelist .paging{
width: 580rpx;
height: 108rpx;
margin: 30rpx auto;
}
.con .prizelist .paging .page_btn{
width: 96rpx;
height: 32rpx;
font-size: 32rpx;
font-family: "PingFangSC";
color: #ffffff;
line-height: 36rpx;
float: left;
margin: auto 23rpx;
}
.con .prizelist .page_num{
font-size: 24rpx;
font-family: "PingFangSC";
color: #ffffff;
line-height: 36rpx;
float: left;
margin: auto 75rpx;
}
有一个可有可无的警告:
Now you can provide attr wx:key for a wx:for to improve performance.
解决办法:添加wx:key属性,
①使用循环的 array 中 item 的某个property,比如 wx:key=“item.id”
此时数组的格式应为:
{id: "1", name: "张三", pic: "../../images/1.png", gift:"小蛋糕"},
②使用数组的下标,即 wx:key=“index”
③ wx:key="*this" 我没太看懂,是
官方文档说的有一个经历过的低级错误:
错误:
onLoad: function () {
setList();
},
改正:
onLoad: function () {
this.setList();
},
(我都没眼看了,马虎或则脑子不清楚的错误总是次次能碰到——2021年3月9日)
后续
1.被指出 “第 X 页 共 X 页”的margin固定,当页数增加到双位数后,下一页被挤到下一行了。
方法1(同事脑力成果,担待了):修改class为page_name的margin为百分比。
即
.con .prizelist .page_num{
margin: auto 75rpx;
}
改为:
.con .prizelist .page_num{
margin: auto 10%;
}
方法2(我自己的老办法):给“上一页”“共 页”“下一个”分别定义class:
<view class="paging">
<view class="up_page" bindtap="up_page" >{{current_page > 1 ? '上一页' : ''}}</view>
<view class="down_page" bindtap="next_page">{{current_page < last_page ? '下一页' : ''}}</view>
<view class="page_num">第{{current_page}}页 共{{last_page}}页</view>
</view>
样式:
.con .prizelist .paging{
width: 100%;
height: 108rpx;
font-size: 32rpx;
font-family: "PingFangSC";
color: #ffffff;
line-height: 36rpx;
text-align: center;
}
.con .prizelist .paging .up_page{
width: 96rpx;
height: 32rpx;
float: left;
margin-left: 72rpx;
}
.con .prizelist .paging .down_page{
width: 96rpx;
height: 32rpx;
float: right;
margin-right: 72rpx;
}
.con .prizelist .page_num{
width: 500rpx;
font-size: 24rpx;
font-family: "PingFangSC";
color: #ffffff;
line-height: 36rpx;
margin: auto;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录前言一、思路分析二、静态页面搭建2.1 结构层2.2 样式层三、js页面交互3.1 获取元素及变量初始化3.2 10个雷的初始化设置3.3 游戏开始事件封装3.4 核心事件函数封装3.5
jquery与javascript如何设置或修改a标签href属性呢?非常的简单,例如小编要修改a标签href属性,将跳转网址改成“www.tpyyes.com”,如下: ahref=www.baidu.comid=myId点击进入百度/a 如果是javascript代码,可以这样写: document.getElementById(myId).se
我们在做后台管理系统时,经常有根据不同权限添加不同路由的需求。对于一些新手老说,比较容易容易遇到的问题就是,动态添加的路由页面在刷新出现时失效的情况。那么这时网页刷新出现404是什么原因呢?我们要如何解决?
javascript中求平方的方法:1、利用“*”乘法运算符,语法“x * x”,参数“x”是需要求平方的数值;2、利用pow()方法,语法“Math.pow(x,2)”。
对象有两种属性,普通的数据属性和访问器属性。访问器属性本质上是用于获取和设置值的函数(可以拦截、过滤、处理等操作要设置或获取的属性),但从外部代码来看就像传统属性一样。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008