「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)
2024-08-28 00:05:08
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-16/
wx:key的高级特性。这个很重要,因为在app上经常有上拉,下拉加载,我们如果不使用这个特性的很可能列表就乱了。源码:https://github.com/limingios/wxProgram.git 中的No.8
小程序的列表的渲染
- 官方的阐述
>https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/list.html
- 演示wx:key
>如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 input 中的输入内容, 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。
wx:key 的值以两种形式提供字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。
保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。
wxKey.wxml
<!wxKey.wxml-->
<view class="container">
<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>
</view>
wxKey.js
//wxKey.js
//获取应用实例
const app = getApp()
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
})
}
})
如果不加入wx:key=”unique” 或者wx:key=”*this” 进行绑定的话,可能存在漂移的情况,这种问题很大,建议在for循环的时候都定义一个唯一的key。
PS:列表需要的注意的很多,基本做企业开发和互联网开发列表展示很常见,也是必须的所以在wxml这块一定要对for循环做好处理,key的绑定。小心漂移。
最新文章
- bzoj2683
- 【原创】android内存管理-hprof文件
- iOS 利用for循环创建九宫格
- 应用在tomcat下的四种部署方式(原创)
- 20个免费的 JavaScript 游戏引擎分享给开发者
- 为你的Windows7设置动态壁纸
- 【java开发系列】— JDOM创建、改动、删除、读取XML文件
- codeblocks常用快捷键
- nodejs 计算内存使用率
- c#中的数据类型简介(数组)
- C++模板:欧拉函数
- 【Java】【Flume】Flume-NG源代码分析的启动过程(两)
- iOS 之 线程和进程
- iptables 完成联网控制 (续) ,独立native进程监听。
- centos7 yum 安装 redis
- TensorFlow 基础知识
- 解决在onCreate()过程中获取View的width和Height为0的方法
- prerender-spa-plugin Vue预渲染配合meta-info优化seo
- 通过c++11的condition_variable实现的有最大缓存限制的队列
- zabbix学习笔记----概念----2019.03.25
热门文章
- [转载][QT][SQL]sql学习记录6_sqlite Autoincrement(自动递增)
- MapReduce工作原理(简单实例)
- Could Not Launch Appium Inspector
- 如何实现一个可以用 await 异步等待的 Awaiter
- BW模型数据删除
- testem方便的web tdd 测试框架使用
- 你真的了解分层架构吗?——写给被PetShop";毒害";的朋友们
- Python 3 利用 Dlib 和 sklearn 人脸笑脸检测机器学习建模
- C++中const使用注意要点(一)
- FPGA能代替CPU架构吗?