微信小程序 --- setData(点赞)

点赞效果:

wxml:

绑定数组元素下标(index)和id,根据接口数据返回是否已点赞状态(like)。

<view class="fabulousBox" bindtap="handleFabulous" data-id="{{item.id}}" data-index="{{index}}">                                                                                          
  <image wx:if="{{item.like}}" class="fabulousImg" src="../../assets/images/fabulous-active.png"></image>
  <image wx:else class="fabulousImg" src="../../assets/images/fabulous.png"></image>
  <text class="fabulousNum">{{item.num}}</text>
</view>

js:

commentData:返回的数组数据, like:是否点赞状态,num:点赞数量。

handleFabulous(event){
  var idx = event.currentTarget.dataset.index;
  var _this1 = this;
  this.$request({
    url: '请求的接口',
    type: 'GET',
    param: {
      id: event.currentTarget.dataset.id,
    }
  }).then(function (res) {
      _this1.__data__.commentData[idx].like = !_this1.__data__.commentData[idx].like;
      _this1.__data__.commentData[idx].num = !_this1.__data__.commentData[idx].like ? _this1.__data__.commentData[idx].num-1 : _this1.__data__.commentData[idx].num+1;
      _this1.setData({
        commentData: _this1.__data__.commentData
      });
  })["catch"](function (error) {
      console.log(error, 'error');
  })
},

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值