el-table 多选框左侧
时间: 2023-10-03 18:04:51 浏览: 42
1. 在 el-table 组件中,可以通过设置 `show-checkbox` 属性来显示多选框。
2. 多选框默认显示在表格的第一列,可以通过设置 `selection-column-width` 属性来调整多选框列的宽度。
3. 如果想要将多选框显示在表格的其他列,可以使用自定义列模板的方式来实现。具体做法如下:
(1) 在 el-table-column 中设置 `type` 属性为 `selection`,表示这是一个多选框列。
(2) 在 el-table-column 中设置 `width` 属性来调整多选框列的宽度。
(3) 使用 slot-scope 来自定义多选框列的内容,具体做法如下:
```
<el-table-column type="selection" width="100">
<template slot-scope="{row}">
<el-checkbox v-model="checkedRows" :label="row"></el-checkbox>
</template>
</el-table-column>
```
在上面的例子中,`checkedRows` 是一个数组,用来存储选中的行数据。在 slot-scope 中,我们可以访问到当前行的数据 `row`,并将其作为多选框的 label 属性绑定到 el-checkbox 上,以便在选中时能够获取到选中的行数据。
相关问题
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 变量中,以便后续使用。