小程序怎样实现列表分页展示及上下页点击
Admin 2022-11-19 群英技术资讯 760 次浏览
具体内容如下
主要实现功能:
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
原生Ajax与JQuery Ajax,有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
Nodejs构建mock数据并通过restapi风格调用接口访问数据如果我们只有json格式的数据文件,我们想通过访问url方式调用居然数据 确保电脑安装node环境如果你没有安装好node环境请移步http://nodejs.cn/ 一、安装json-server 1.新建demo文件cddemo 2.安装json-server npminstall-Sjson-
前言过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它。事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊JS中 reduce() 的用法。一、语法arr.reduce(function(prev,cur,index,arr){...}, i
JavaScript中怎么实现继承?下面本篇文章给大家分享JS实现继承的6种方法,希望对大家有所帮助!
vue是前端轻量级MVVM框架,入门门槛相对较低,今天用Vue做一个购物车实例,所以下面这篇文章主要给大家介绍了关于vue实现购物车全部功能的简单方法,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008