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

效果

代码

wxml

html 复制代码
<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

css 复制代码
/* 列表 */
.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

javascript 复制代码
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
    })
  },
})
相关推荐
陈随易43 分钟前
Postman 被曝自动往 Claude Code 和 Codex 装插件
前端·后端·程序员
Csvn4 小时前
性能监控
前端
Csvn4 小时前
构建工具
前端
FRing035 小时前
vue2干货总结3(computed计算属性、watch监听器)
开发语言·前端·vue.js·vue2
kyriewen7 小时前
Skills 榜前 10 我全装了:只有 3 个能直接用
前端·javascript·程序员
IT_陈寒7 小时前
当Java的hashCode遇上Lombok,我的缓存被全清了
前端·人工智能·后端
工控丫丫8 小时前
帆软看板中经常使用的公式函数及经常搭的组合
前端·javascript·数据库
jerrywus8 小时前
我用 GPT-6.1-sol 做了台会讲解的 3D 售货机:60 罐饮料,一口没喝上
前端·agent·claude
孟陬9 小时前
Foobar 技术周刊#159:当看不懂代码时,如何高效排障调试。脏页陷阱:一条只读查询,写入数 TB 数据。推测解码:大模型加速利器
前端·人工智能·后端
Zhao-Jichao9 小时前
AI Prompting for Everyone 课程笔记 - P17 图像生成 Imaging Generation
前端