1.列表(本部分內(nèi)容出入官方文檔)
對(duì)于這個(gè)功能,微信小程序中并沒(méi)有提供類似于Android中l(wèi)istview性質(zhì)的控件,所以我們需要使用 wx:for 控制屬性綁定一個(gè)數(shù)組,用數(shù)組中各項(xiàng)的數(shù)據(jù)重復(fù)渲染該組件,來(lái)達(dá)到列表的效果。
<view wx:for="{{array}}"> {{index}}: {{item.message}}</view>Page({ data: { array: [{ message: 'foo', }, { message: 'bar' }] }})默認(rèn)數(shù)組的當(dāng)前項(xiàng)的下標(biāo)變量名默認(rèn)為index,數(shù)組當(dāng)前項(xiàng)的變量名默認(rèn)為item,當(dāng)然也可以通過(guò) wx:for-item 和 wx:for-index 指定。
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName"> {{idx}}: {{itemName.message}}</view>wx:for也可以嵌套,下邊是一個(gè)九九乘法表
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i"> <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j"> <view wx:if="{{i <= j}}"> {{i}} * {{j}} = {{i * j}} </view> </view></view>block wx:for
類似block wx:if,也可以將wx:for用在<block/>標(biāo)簽上,以渲染一個(gè)包含多節(jié)點(diǎn)的結(jié)構(gòu)塊。例如:
<block wx:for="{{[1, 2, 3]}}"> <view> {{index}}: </view> <view> {{item}} </view></block>wx:key
如果列表中項(xiàng)目的位置會(huì)動(dòng)態(tài)改變或者有新的項(xiàng)目添加到列表中,并且希望列表中的項(xiàng)目保持自己的特征和狀態(tài)(如 <input/> 中的輸入內(nèi)容,<switch/> 的選中狀態(tài)),需要使用 wx:key 來(lái)指定列表中項(xiàng)目的唯一的標(biāo)識(shí)符。
wx:key 的值以兩種形式提供
1. 字符串,代表在 for 循環(huán)的 array 中 item 的某個(gè) property,該 property 的值需要是列表中唯一的字符串或數(shù)字,且不能動(dòng)態(tài)改變。
2. 保留關(guān)鍵字 *this 代表在 for 循環(huán)中的 item 本身,這種表示需要 item 本身是一個(gè)唯一的字符串或者數(shù)字,如:
當(dāng)數(shù)據(jù)改變觸發(fā)渲染層重新渲染的時(shí)候,會(huì)校正帶有 key 的組件,框架會(huì)確保他們被重新排序,而不是重新創(chuàng)建,以確保使組件保持自身的狀態(tài),并且提高列表渲染時(shí)的效率。
如不提供 wx:key,會(huì)報(bào)一個(gè) warning, 如果明確知道該列表是靜態(tài),或者不必關(guān)注其順序,可以選擇忽略。
示例代碼:
<switch wx:for="{{objectArray}}" wx:key="unique" style="display: block;"> {{item.id}} </switch><button bindtap="switch"> Switch </button><button bindtap="addToFront"> Add to the front </button><switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch><button bindtap="addNumberToFront"> Add to the front </button>Page({ data: { objectArray: [ {id: 5, unique: 'unique_5'}, {id: 4, unique: 'unique_4'}, {id: 3, unique: 'unique_3'}, {id: 2, unique: 'unique_2'}, {id: 1, unique: 'unique_1'}, {id: 0, unique: 'unique_0'}, ], numberArray: [1, 2, 3, 4] }, switch: function(e) { const length = this.data.objectArray.length for (let i = 0; i < length; ++i) { const x = Math.floor(Math.random() * length) const y = Math.floor(Math.random() * length) const temp = this.data.objectArray[x] this.data.objectArray[x] = this.data.objectArray[y] this.data.objectArray[y] = temp } this.setData({ objectArray: this.data.objectArray }) }, addToFront: function(e) { const length = this.data.objectArray.length this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray) this.setData({ objectArray: this.data.objectArray }) }, addNumberToFront: function(e){ this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray) this.setData({ numberArray: this.data.numberArray }) }})2.下拉刷新
小程序頁(yè)面集成了下拉功能,并提供了接口,我們只需要一些配置就可以拿到事件的回調(diào)。
1. 需要在 .json 文件中配置。(.json 文件的格式和 app.json與具體頁(yè)面的.json文件的區(qū)別,前文已經(jīng)講過(guò),有疑問(wèn)的可以移步。) 如果配置在app.json文件中,那么整個(gè)程序都可以下拉刷新。如果寫(xiě)在具體頁(yè)面的.json文件中,那么就是對(duì)應(yīng)的頁(yè)面,可以下拉刷新。
具體頁(yè)面的.json文件:
{ "enablePullDownRefresh": true}app.json文件:
"window": { "enablePullDownRefresh": true }2. 在js文件中添加回調(diào)函數(shù)
// 下拉刷新回調(diào)接口 onPullDownRefresh: function () { // do somthing },3. 添加數(shù)據(jù)
通常情況下的下拉刷新操作,就是把查詢條件重置,讓頁(yè)面顯示最新的一頁(yè)數(shù)據(jù)。下面是筆者demo中的下拉刷新回調(diào)接口的代碼,同時(shí)也是一般情況下的操作流程。
// 下拉刷新回調(diào)接口 onPullDownRefresh: function () { // 我們用total和count來(lái)控制分頁(yè),total代表已請(qǐng)求數(shù)據(jù)的總數(shù),count代表每次請(qǐng)求的個(gè)數(shù)。 // 刷新時(shí)需把total重置為0,代表重新從第一條請(qǐng)求。 total = 0; // this.data.dataArray 是頁(yè)面中綁定的數(shù)據(jù)源 this.data.dataArray = []; // 網(wǎng)絡(luò)請(qǐng)求,重新請(qǐng)求一遍數(shù)據(jù) this.periphery(); // 小程序提供的api,通知頁(yè)面停止下拉刷新效果 wx.stopPullDownRefresh; },3 .上拉加載
同下拉刷新一樣,小程序中也提供了用于上拉時(shí)回調(diào)的接口。官方文檔中并沒(méi)有很詳細(xì)的介紹,經(jīng)測(cè)試發(fā)現(xiàn),上拉回調(diào)的接口并不需要額外的配置(下拉時(shí)需要在 .json文件中配置 "enablePullDownRefresh": true),直接在頁(yè)面滑動(dòng)到底部時(shí)就能拿到回調(diào)。
1. 在js文件中添加回調(diào)函數(shù)
// 上拉加載回調(diào)接口 onReachBottom: function () { // 我們用total和count來(lái)控制分頁(yè),total代表已請(qǐng)求數(shù)據(jù)的總數(shù),count代表每次請(qǐng)求的個(gè)數(shù)。 // 上拉時(shí)需把total在原來(lái)的基礎(chǔ)上加上count,代表從count條后的數(shù)據(jù)開(kāi)始請(qǐng)求。 total += count; // 網(wǎng)絡(luò)請(qǐng)求 this.periphery(); },以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注