實現點贊效果,動態刷新條目小心的狀態
實現效果
一修改鍵值對:
wxml:
<view class="list-item" wx:for="{{articleList}}">
<navigator url="/pages/webdetailPage/webdetail?url={{item.link}}" hover-class="navigator-hover">
<view class="list-item-text-title">
<image src="/img/ic_user.png" class='userhead'></image>
<text style='font-size: 13px;'>{{item.author}}</text>
<text class='item_time'>{{item.niceDate}}</text>
</view>
<view style='margin-left: 25rpx; margin-right: 45rpx;'>
<text class='item_content'>{{item.title}}</text>
</view>
<view style='margin-bottom: 15rpx; margin-top: 35rpx'>
<text style='font-size: 13px; color: #1296db; margin-left: 15rpx'>{{item.chapterName}}</text>
<block wx:if="{{item.zan > 0}}" >
<image src="/img/love.png" class='userlove' data-love="{{index}}" catchtap='love_click'></image>
</block >
<block wx:else>
<image src="/img/love_no.png" class='userlove' data-love="{{index}}" catchtap='love_click'></image>
</block >
</view>
</navigator>
</view>
JS:
love_click:function(e){
var i = e.currentTarget.dataset.love //獲取所點擊item的index
var that = this;
var zan = "articleList[" + i + "].zan";//先用一個變量,把(articleList[0].zan)用字符串拼接起來
that.setData({
[zan]: 1
})
}
新增鍵值對:
wxml:
<view class="list-item" wx:for="{{articleList}}">
<navigator url="/pages/webdetailPage/webdetail?url={{item.link}}" hover-class="navigator-hover">
<view class="list-item-text-title">
<image src="/img/ic_user.png" class='userhead'></image>
<text style='font-size: 13px;'>{{item.author}}</text>
<text class='item_time'>{{item.niceDate}}</text>
</view>
<view style='margin-left: 25rpx; margin-right: 45rpx;'>
<text class='item_content'>{{item.title}}</text>
</view>
<view style='margin-bottom: 15rpx; margin-top: 35rpx'>
<text style='font-size: 13px; color: #1296db; margin-left: 15rpx'>{{item.chapterName}}</text>
<block wx:if="{{item.love> 0}}" >
<image src="/img/love.png" class='userlove' data-love="{{index}}" catchtap='love_click'></image>
</block >
<block wx:else>
<image src="/img/love_no.png" class='userlove' data-love="{{index}}" catchtap='love_click'></image>
</block >
</view>
</navigator>
</view>
JS:
love_click:function(e){
//新增一個鍵值對
var islove = "articleList[" + i + "].islove"
this.setData({
[islove]:1
})
}
數值修改結果頁