微信小程序:实现列表单选

 效果

代码

 wxml

<view class="all">
  <view class="item_all" wx:for="{{info}}" wx:key="index">
    <view class='position {{item.checked?"checked_parameter":""}}' data-id="{{item.employee_num}}" bindtap='selectcustomer'>
      <view class="vv_1">{{item.num_name}}</view>
    </view>
  </view>
</view>

wxss

/* 列表 */
.item_all {
  /* border: 1px solid black; */
  margin-bottom: 3%;
  width: 100%;
}

.position {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 80px;
  border-radius: 10px;
  background-color: #fff;
  box-shadow: 2px 2px 2px gainsboro;
}

.vv_1 {
  margin-left: 5%;
  word-break: break-all;
}

/* 选中之后的样式设置 */
.checked_parameter {
  background-color: #74bfe7;
  color: #fff;
}

js 

const app = getApp()
Page({
  data: {
    info: [{
      employee_num: 1001,
      employee_name: '张三',
      checked: false,
      num_name: '1001-张三'
    },
    {
      employee_num: 1002,
      employee_name: '李四',
      checked: false,
      num_name: '1002-李四'
    }, {
      employee_num: 1003,
      employee_name: '王五',
      checked: false,
      num_name: '1003-王五'
    }, {
      employee_num: 1004,
      employee_name: '赵六',
      checked: false,
      num_name: '1004-赵六'
    }
  ],
  parameterList: ''
  },
  // 参数点击响应事件
  selectcustomer: function (e) {
    var this_checked = e.currentTarget.dataset.id //获取对应的条目id
    var parameterList = this.data.info //获取Json数组
    console.log(this_checked)
    for (var i = 0; i < parameterList.length; i++) {
      if (parameterList[i].employee_num == this_checked) {
        parameterList[i].checked = true; //当前点击的位置为true即选中
        this.setData({
          parameterList:parameterList[i]
        })
        console.log('参数', this.data.parameterList)
      } else {
        parameterList[i].checked = false; //其他的位置为false
      }
    }
    this.setData({
      info:parameterList
    })
  },
})

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值