//index.js
//获取应用实例
const app = getApp()
Page({
data: {
timeLine: app.globalData.timeline.data,
showMenu: app.globalData.timeline.show
},
//生命周期函数,页面加载完毕后执行
onLoad: function() {
console.log('timeline onLoad')
console.log(this.data.timeLine)
},
//methods方法
touchEntry: function(e) {
wx.navigateTo({
url: '../entry/entry?index=' + e.currentTarget.dataset.index
})
},
bindButtonTapSheet: function() {
var that = this;
var showMenu = that.data.showMenu;
if (showMenu == false) {
showMenu = true;
} else {
showMenu = false;
}
that.setData({
showMenu: showMenu
})
},
showLike: function(ev) {
var that = this;
var index = ev.currentTarget.dataset.index;
var timeLine = that.data.timeLine;
if (timeLine[index].like == false) {
timeLine[index].like = true;
} else {
timeLine[index].like = false;
}
that.setData({
timeLine: timeLine
})
}
})
<view class='time-card-action' bindtap='showLike' data-index="{{itemIndex}}">
<image style='width: 50rpx; height: 38rpx; margin-top: 15rpx; margin-left: 10rpx;' src='http://covteam.u.qiniudn.com/heart2.png' wx:if="{{item.like == false}}"></image>
<image style='width: 50rpx; height: 38rpx; margin-top: 15rpx; margin-left: 10rpx;' src='/image/like.png' wx:if="{{item.like == true}}"></image>
</view>
- this指向的是page里面的所有内容;
- 当WXML需要遍历数组进行列表渲染的时候,若要获得数组的index下标,首先要在标签里传入数据的下标,data-index="{{itemIndex}}",再在js方法中通过ev.currentTarget.dataset.index去获取下标,再根据下标索引对data数组里的每一项元素里面的内容进行修改;
- 当所有数据修改完毕后,记得用this.setData的方法进行数据的更新。