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

實現點贊效果,動態刷新條目小心的狀態


實現效果

一修改鍵值對:

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閱讀 172,846評論 25 708
  • 轉載鏈接 注:本文轉載知乎上的回答 作者:初雪 鏈接:https://www.zhihu.com/question...
    pengshuangta閱讀 28,651評論 9 295
  • 2017.1.2 2017年的第一篇字,寫個莫名其妙的東西。 一直以來喜歡貓或喜歡狗的人都被分成兩派,也不排除什么...
    肥貓半仙閱讀 500評論 0 2
  • 各路煙火 埋伏于覆蓋我的黑夜 月亮和火柴 誰將率先燃燒 照徹道路的分野 人們忙于擁抱 微笑 卻視而不見流轉的季節 ...
    北十字星閱讀 211評論 0 0