您好,登錄后才能下訂單哦!
這篇“微信小程序怎么實(shí)現(xiàn)列表分頁功能”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“微信小程序怎么實(shí)現(xiàn)列表分頁功能”文章吧。
主要實(shí)現(xiàn)功能:
1.列表展示
2.上下頁點(diǎn)擊
效果圖:
知識點(diǎn):wx:for、bindtap、生命周期函數(shù)–監(jiān)聽頁面加載、.filter、取余( %
)取整(parseInt(x/y)
)函數(shù)
js
data: { frontPage: false,//上一頁 存在true,不存在false nextPage: false,//下一頁 存在true,不存在false pages: 0,//所有頁 thisPages: 0,//當(dāng)前頁 rows: 6,//每頁條數(shù) total: 0,//總條數(shù) pageData: [],//本頁顯示的列表數(shù)據(jù) 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', },
/** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ 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) { //元素值,元素的索引,原數(shù)組。 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; //當(dāng)前頁 if ( pages > 1){ that.setData({ nextPage: true, }) } that.setData({ thisPages: thisPages, pageData: pageData, pages: pages, rows: rows, }) } },
//點(diǎn)擊下一頁 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) { //元素值,元素的索引,原數(shù)組。 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, }) },
//點(diǎn)擊上一頁 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) { //元素值,元素的索引,原數(shù)組。 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; }
有一個(gè)可有可無的警告:
Now you can provide attr wx:key
for a wx:for
to improve performance.
解決辦法:添加wx:key屬性,
①使用循環(huán)的 array 中 item 的某個(gè)property
,比如 wx:key=“item.id”
此時(shí)數(shù)組的格式應(yīng)為:
{id: "1", name: "張三", pic: "../../images/1.png", gift:"小蛋糕"},
②使用數(shù)組的下標(biāo),即 wx:key=“index”
③ wx:key="*this" 我沒太看懂,是
官方文檔說的有一個(gè)經(jīng)歷過的低級錯(cuò)誤:
錯(cuò)誤:
onLoad: function () { setList(); },
改正:
onLoad: function () { this.setList(); },
后續(xù)
1.被指出 “第 X 頁 共 X 頁”的margin固定,當(dāng)頁數(shù)增加到雙位數(shù)后,下一頁被擠到下一行了。
方法1(同事腦力成果,擔(dān)待了):修改class為page_name的margin為百分比。
即
.con .prizelist .page_num{ margin: auto 75rpx; }
改為:
.con .prizelist .page_num{ margin: auto 10%; }
方法2(我自己的老辦法):給“上一頁”“共 頁”“下一個(gè)”分別定義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; }
以上就是關(guān)于“微信小程序怎么實(shí)現(xiàn)列表分頁功能”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。