「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)
转自: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的绑定。小心漂移。
「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)的更多相关文章
- 「小程序JAVA实战」小程序的表单组件(25)
转自:https://idig8.com/2018/08/18/xiaochengxujavashizhanxiaochengxudebiaodanzujian25/ 来说下 ,小程序的基础组件.源码 ...
- 「小程序JAVA实战」小程序的flex布局(22)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-22/ 之前已经把小程序的框架说完了,接下来说说小程序的组件,在说组件之前,先说说布局吧.源码:ht ...
- 「小程序JAVA实战」小程序视图之细说列表渲染(14)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-14/ 列表的渲染,不管是任何语言都有列表这个概念.源码:https://github.com/li ...
- 「小程序JAVA实战」小程序的留言和评价功能(70)
转自:https://idig8.com/2018/10/28/xiaochengxujavashizhanxiaochengxudeliuyanhepingjiagongneng69/ 目前小程序这 ...
- 「小程序JAVA实战」小程序的举报功能开发(68)
转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66-2/ 通过点击举报 ...
- 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...
- 「小程序JAVA实战」小程序的关注功能(65)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeguanzhugongneng64/ 在个人页面,根据发布者个人和 ...
- 「小程序JAVA实战」小程序的视频点赞功能开发(62)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeshipindianzangongnengkaifa61/ 视频点 ...
- 「小程序JAVA实战」小程序的springboot后台拦截器(61)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudespringboothoutailanjieqi60/ 之前咱们把 ...
随机推荐
- C的文件操作函数
fgetc(FILE *)意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节fputc(char,FILE*)将字符ch写到文件指针fp所指向的文件的当前写指针的 ...
- Codeforces 1012C Hills【DP】*
Codeforces 1012C Hills Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suff ...
- (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
if (this == null) Console.WriteLine("this is null"); 这句话一写,大家一定觉得荒谬,然而 if 内代码的执行却是可能的!本文讲介 ...
- LeetCode Friend Circles
原题链接在这里:https://leetcode.com/problems/friend-circles/description/ 题目: There are N students in a clas ...
- C#读取Excel数据操作大全
苦丁茶 发表于 2014-02-10 12:58:00 | 分类标签: ASP.NET 读取Excel 本文介绍下,用C#读取excel数据的例子,包括读取整个工作薄的数据.读取工作薄选定区域中的数据 ...
- hdu4261 Estimation[暴力dp+对顶堆]
https://vjudge.net/problem/HDU-4261 对于一个长2000的数列划分最多25个块,每块代价为块内每个数与块内中位数差的绝对值之和,求最小总代价. 套路化地,设$f[i] ...
- 理想中的 PCB 文件格式
理想中的 PCB 文件格式 因为平时写代码使用 git,在画 PCB 也使用 git 来管理 PCB 文件. 但是 PCB 文件是二进制的,所以在比较时非常麻烦. 虽然 PCB 文件可以导出 文本文件 ...
- 嵌入式linux问题杂锦
tftp 在开发板上不能获取共享文件,出现: Permission denied tftp: can't open 'myTcpTest': Permission denied 是因为,我在/sys目 ...
- 抽象工厂模式(abstract)创建型模式
(一)简单工厂模式? 现在的学习是面向对象面向接口的,但是执行时的操作需要实例化后的对象.随着我们需要的类的增加,我们就需要把这些共同的东西提取出来,放在一个抽象类中,让这些子类来继承抽象类.当我们调 ...
- Android Java访问本地方法(JNI)
当功能需要本地代码实现的时候,Java 代码就需要调用本地代码. 在调用本地代码时,首先要保证本地代码被加载到 Java 执行环境中并与 Java 代码连接在一起,这样 Java 代码在调用本地方法时 ...