微信小程序如何修改以及新增值对

实现点赞效果,动态刷新条目小心的状态


实现效果

一修改键值对:

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
    })
}
数值修改结果页
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,388评论 25 708
  • 转载链接 注:本文转载知乎上的回答 作者:初雪 链接:https://www.zhihu.com/question...
    pengshuangta阅读 28,763评论 9 295
  • 2017.1.2 2017年的第一篇字,写个莫名其妙的东西。 一直以来喜欢猫或喜欢狗的人都被分成两派,也不排除什么...
    肥猫半仙阅读 503评论 0 2
  • 各路烟火 埋伏于覆盖我的黑夜 月亮和火柴 谁将率先燃烧 照彻道路的分野 人们忙于拥抱 微笑 却视而不见流转的季节 ...
    北十字星阅读 211评论 0 0