
这可以这样理解小程序已经把数据和视图绑定了起来,众所周知vue是数据双向绑定的,数据改变,视图也会相应的改变,而上面的那段话是不是可以理解为小程序也是双向绑定的,
在vue里修改数据可以写 this.a = b
小程序里修改数据需要 this.setData({"key":value})
那我们是不是可以这样理解在小程序修改数据的时候只是调用setData方法修改了定义中的变量,
跟vue中this.a = b 是一样的性质而只是写法不一样
明白了,这一点,说解决方法。。。
上面说了数据量大的时候不能用concat的原因,那我们这时候该怎么解决,只是我们可以想,既然数据和视图绑定在一起了,我们是不是可以用一个二维数组解决这个问题
大概思路是这样的
1,在data里定义一个空数组,
2,获取下拉数据
3,把获取的数组,this.setData上面定义的数组
例如this.setData({
[`arr[${b}]`]:res.data
})
这样就避免了setData数据过大而不报错的问题。
这时候数组发生了改变视图也会相应的改变,不过这样可能会出现一个问题,如果加载数据过多的时候视图会出现渲染层失败,
1、参数接受一个对象,以key,value的形式表示;2、参数和变量名称一致,可用一个值代替(es6新语法特性)
3、可以设置一个或多个data数据.
4、key可以以数据路径的形式给出(路径形式的key必须带引号)(经小程序更新后,现已支持不加引号的写法)
5、key值可以为变量,为变量的时候要用[ ]引起来
注意:deletedtodo为变量作为key值的时候要用 [] 包裹起来。
6、直接修改this.data,虽然会改变数据,但是页面不会重新渲染,无法改变页面状态,会造成数据不一致的情况
7、单次设置的数据不能超过1024KB,请尽量避免一次设置过多的数据
8、不需要在this.data中预先定义,使用setData()方法会自动创建该数据
name在this.data中未定义,但是这种写法不会报错,而且还会在this.data.zhong创建name这条数据。可以用this.data.name获取到该条数据
1、数组的增加:
我们可以自己组装一个对象如上图的obj,然后用push()方法将其添加到数据的最后。注意push的数据的index是+1的,也就是说原本数组中index依次为0,1,2,新增加的就是3,依次类推。如果想将数据插入到数据某个位置,那么可以合理运用concat()的方法将数据合并到数据中。
也可以用splice()方法添加,第一个参数是插入的位置,第二个参数设为0,则为添加,若为大于0的的数字则为要删除的个数,第三个参数只有在第二个参数为0是使用,是添加的内容。这种方法非常灵活,只需要改变第一个参数就可以将内容添加到数据的任何一个地方。下面附上splice的用法;
2、数组的删除:
删除也是用splice()方法实现的,ceshi.splice(1,1)就是从index为1的位置开始,删除1个元素。splice()用法参上。
3、数组的修改:
将key值以数据路径的形式赋值,可以达到修改数据中的某一条,此处,我们只将ceshi[0].value的值改变为'oooo',其他数据未发生变化。
也可以用这种方法修改数组的参数,对这种方法有疑问的可以参考setData()第二条。
出自 https://zhuanlan.zhihu.com/p/59762438](https://zhuanlan.zhihu.com/p/59762438
<view class="sort">
<view class="imageLi {{index==current?'moving':'normal'}}" wx:for="{{imageList}}" data-index="{{index}}" wx:key="{{index}}" bindtouchmove="move" bindtouchstart="movestart" bindtouchend="moveend" style="left:{{index==current?move_x * 2:item.left*2}}rpxtop:{{index==current?move_y:item.top}}px">
<image data-index="{{index}}" src="{{item.src}}" />
</view>
</view>
/* pages/sort/sort.wxss */
.sort{
padding:60rpx 60rpx
position:relative
}
.imageLi{
width:120rpx
height:160rpx
display: inline-block
position:absolute
z-index: 99
}
.imageLi image{
width:100%
height:100%
}
.imageLi:nth-child(4n){
margin-right:0
}
// pages/sort/sort.js
var x, y, x1, y1, x2, y2
Page({
/**
/**
//添加信息
changeMove: function (array) {
const self = this
var width = self.data.all_width, _w = 0, row = 0, column = 0
var arr = [].concat(array)
arr.forEach(function (n, i) {
n.left = (self.data.u_w + self.data.s_h) * row + self.data.s_h
n.top = (self.data.u_h + self.data.s_v) * column + self.data.s_v
n._left = n.left
n._top = n.top
_w += self.data.u_w + self.data.s_h
if (_w + self.data.u_w + self.data.s_h >width) {
_w = 0
row = 0
column++
} else {
row++
}
})
console.log(arr)
return arr
},
movestart: function (e) {
x = e.touches[0].clientX
y = e.touches[0].clientY
x1 = e.currentTarget.offsetLeft
y1 = e.currentTarget.offsetTop
this.setData({
current: e.currentTarget.dataset.index,
move_x: x1,
move_y: y1
})
},
move: function (e) {
var self = this
// // console.log('move',e.target.dataset.current)
x2 = e.touches[0].clientX - x + x1
y2 = e.touches[0].clientY - y + y1
// // this.setData({
// // current: currindex,
// // start: { x: x2, y: y2 }
// // })
var underIndex = self.getCurrnetUnderIndex()
},
moveend: function (e) {
const that = this
that.setData({
current: -1,
})
},
changeArrayData: function (arr, i1, i2) {
var temp = arr[i1]
arr[i1] = arr[i2]
arr[i2] = temp
},
getCurrnetUnderIndex: function (endx, endy) {//获取当前移动下方index
var endx = x2 + this.data.u_w / 2,
endy = y2 + this.data.u_h / 2
var v_judge = false, h_judge = false, column_num = (this.data.all_width - this.data.s_h) / (this.data.s_h + this.data.u_w) >>0
// console.log(endx,endy)
var _column = (endy - this.data.s_v) / (this.data.u_h + this.data.s_v) >>0
var min_top = this.data.s_v + (_column) * (this.data.u_h + this.data.s_v),
max_top = min_top + this.data.u_h
// console.log('v', _column, endy, min_top, max_top)
if (endy >min_top &&endy <max_top) {
v_judge = true
}
var _row = (endx - this.data.s_h) / (this.data.u_w + this.data.s_h) >>0
var min_left = this.data.s_h + (_row) * (this.data.u_w + this.data.s_h),
max_left = min_left + this.data.u_w
// console.log('x', _row, endx, min_left, max_left)
if (endx >min_left &&endx <max_left) {
h_judge = true
}
if (v_judge &&h_judge) {
var index = _column * column_num + _row
if (index >this.data.imageList.length - 1) {//超过了
return null
} else {
return index
}
} else {
return null
}
},
/**
},
/**
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)