微信小程序操作数组对象

时间:2019-08-21 00:12:43   收藏:0   阅读:82
navinfo: [{ ‘NavName‘: ‘定位‘, ‘NavIcon‘: ‘location‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },
{ ‘NavName‘: ‘电池‘, ‘NavIcon‘: ‘shopping-cart‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },
{ ‘NavName‘: ‘救援‘, ‘NavIcon‘: ‘phone-circle‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘1‘ },
{ ‘NavName‘: ‘我的‘, ‘NavIcon‘: ‘manager‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ }]
如上代码,需要修改navinfo数组里面的第一个对象{ ‘NavName‘: ‘定位‘, ‘NavIcon‘: ‘location‘, ‘NavIconSize‘: ‘22px‘, ‘IconColor‘: ‘#000‘, ‘IconCheckedColor‘: ‘#fff‘, ‘IsChecked‘: ‘0‘ },里的IsChecked为1,其他的子项的IsChecked属性修改为0,
代码:
 _onChange:function(e){
      
      let that=this;
      var _index=e.currentTarget.dataset.index;
      for (let nav in that.properties.NavInfo){
        if (nav == _index){
          that.setData({
            [‘NavInfo[‘ + nav + ‘].IsChecked‘]:1
          });
        }else{
          that.setData({
            [‘NavInfo[‘ + nav + ‘].IsChecked‘]: 0
          });
        }
      }
    }

以上函数中的e,由前端的data-index中获取,页面代码如下:

<view wx:for="{{NavInfo}}" wx:for-item="Item" style="">wx:key="*this" data-index="{{index}}" bindtap="_onChange" class="navItem">
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!