LAYUI 数据表格 复选,并返回选中行的某一字段值
时间: 2024-05-02 08:21:28 浏览: 10
1. 在数据表格中添加复选框列:
在数据表格的列配置中添加一列,指定类型为“checkbox”,并设置LAY_CHECKED为false即可:
```javascript
cols: [
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'ID', width: 80, sort: true},
{field: 'username', title: '用户名', width: 120},
{field: 'email', title: '邮箱', minWidth: 150},
{field: 'sex', title: '性别', width: 80, sort: true},
{field: 'city', title: '城市', width: 100},
{field: 'sign', title: '签名', minWidth: 160},
{field: 'experience', title: '积分', width: 80, sort: true},
{field: 'score', title: '评分', width: 80, sort: true},
{field: 'classify', title: '职业', minWidth: 100},
{field: 'wealth', title: '财富', width: 135, sort: true},
{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]
```
2. 获取选中行的数据:
在数据表格的配置中,添加一个layEvent事件监听,监听checkbox操作事件,获取选中的数据:
```javascript
table.on('checkbox(test)', function(obj){
var checkStatus = table.checkStatus('test');
console.log(checkStatus.data); //获取选中行的数据
});
```
3. 获取选中行的某一字段值:
在获取选中行的数据后,我们可以循环遍历选中的数据,获取每一行数据的指定字段值:
```javascript
table.on('checkbox(test)', function(obj){
var checkStatus = table.checkStatus('test');
var ids = []; //存储选中行的id值
layui.each(checkStatus.data, function(index, item){
ids.push(item.id); //获取选中行的id值
});
console.log(ids);
});
```
以上代码中,我们定义了一个ids数组,用来存储选中行的id值。然后,我们使用layui.each()方法循环遍历选中的数据,并将每一行数据的id值存储到ids数组中。最后,我们打印出ids数组,即可查看选中行的id值了。