el-table 多选框条件禁选
时间: 2023-11-23 21:54:17 浏览: 54
在 el-table-column type 类型为 selection 组件中,可以通过添加 :selectable='方法名' 来实现多选框条件禁选。具体实现方法是在方法函数中判断需要禁选的条件,例如禁用 order_status 为 1 的项,可以在 selectEnable 函数中添加判断语句,如果 row.order_status 等于 1,则返回 true,否则返回 false。这样就可以实现禁用多选框的功能。另外,如果需要回显多选框,可以在调用接口时传入一个参数 value,然后在获取到数据后,根据 value 的值来决定是否回显多选框。具体实现方法可以参考上述代码中的 getRecruitmentPostList 函数。
相关问题
el-table多选框
el-table 多选框可以通过设置 `selection` 属性来实现。具体操作如下:
1. 在 `el-table` 标签上设置 `@selection-change` 属性,指向一个方法,用于监听多选框的变化。
2. 在 `el-table-column` 标签上设置 `type` 属性为 `'selection'`,表示该列是多选框列。
示例代码如下:
```html
<template>
<el-table :data="tableData" @selection-change="handleSelectionChange">
<el-table-column type="selection"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 18, address: '北京市' },
{ name: '李四', age: 20, address: '上海市' },
{ name: '王五', age: 22, address: '广州市' },
],
selectedRows: [], // 用于存储选中的行数据
};
},
methods: {
handleSelectionChange(selectedRows) {
this.selectedRows = selectedRows;
},
},
};
</script>
```
在上面的示例中,`selectedRows` 数组用于存储选中的行数据,`handleSelectionChange` 方法用于监听多选框的变化,并将选中的行数据存储到 `selectedRows` 数组中。
el-table多选框实现单选
可以通过监听 el-table 的 @row-click 事件来实现 el-table 多选框的单选功能。具体步骤如下:
1.在 el-table 中添加 type="selection",开启多选框功能。
2.在 el-table 中添加 @row-click="handleRowClick" 监听行点击事件。
3.在 methods 中添加 handleRowClick 方法,通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为选中项。
下面是示例代码:
```html
<template>
<el-table
ref="table"
:data="tableData"
@row-click="handleRowClick"
style="width: 100%">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="date"
label="日期"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2021-01-01',
name: '张三',
address: '北京市朝阳区'
}, {
date: '2021-01-02',
name: '李四',
address: '上海市浦东新区'
}, {
date: '2021-01-03',
name: '王五',
address: '广州市天河区'
}],
selection: []
}
},
methods: {
handleRowClick(row) {
this.$refs.table.toggleRowSelection(row)
this.selection = this.$refs.table.selection
}
}
}
</script>
```
在上面的代码中,我们通过监听 @row-click 事件来实现了 el-table 多选框的单选功能。在 handleRowClick 方法中,我们通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为选中项。最后,我们将选中的数据保存在 selection 变量中,以便后续使用。