小程序循环实现单个显示隐藏(秒会)

小程序循环实现单个显示隐藏

获取小程序循环里面的值,实现单个隐藏显示,废话不多说,直接上代码

.wxml

<view bindtap="clase" wx:for="{{content}}" wx:key="{{content}}" data-id="{{item.id}}"><!--把ID传过去-->
    1
   <view hidden="{{item.insert?'true':''}}">{{item.uname}}</view><!-- 默认为true隐藏 点击的时候为null就是控制显示 -->
</view>

.js

Page({
  data: {
    content:[
      {
        id:1,
        uname:'测试1'
      },
      {
        id:2,
        uname:'测试2'
      }
    ]
  },
  clase: function (e) {
    let that = this
    let index = 0;
    let array = this.data.content;//获取循环数组对象
    for (let item of array) {
      //如果当前点击的对象id和循环对象里的id一致
      if (item.id == e.currentTarget.dataset.id) {
        //判断当前对象中的insert是否为true(true为显示,其他为隐藏) insert是新增的一个值然后进行判断
        if (array[index].insert == "" || array[index].insert == undefined) {
          array[index].insert = "true"
        } else {
          array[index].insert = ""
        }
      }
      index++;
    }
    //将数据动态绑定 
    that.setData({
      content: array
    })
  },
})

代码非常少,主要看注释,方便理解,有什么问题欢迎评论留言,我会及时回复你的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值