Vue3用element-plus穿梭框进行的修改成table表格穿梭框
时间: 2023-07-18 18:04:59 浏览: 1423
可以考以下步骤将 Vue3 中的 Element Plus 穿梭框转换为表格穿梭框:
1. 创建表格及其数据
首先,需要创建一个表格及其数据,例如:
```html
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="name" label="Name"></el-table-column>
<el-table-column prop="age" label="Age"></el-table-column>
<el-table-column prop="address" label="Address"></el-table-column>
</el-table>
```
```javascript
data() {
return {
tableData: [
{ name: 'John', age: 20, address: 'New York' },
{ name: 'Mary', age: 22, address: 'Los Angeles' },
{ name: 'Tom', age: 25, address: 'Chicago' }
],
leftList: [],
rightList: []
};
}
```
注意,这里还定义了 `leftList` 和 `rightList` 数组,用于存储穿梭框的选中项。
2. 创建表格穿梭框
接下来,需要创建一个包含两个穿梭框和操作按钮的组件,例如:
```html
<template>
<div class="table-transfer">
<div class="table-transfer-left">
<el-table
ref="leftTable"
:data="leftList"
style="width: 100%"
@selection-change="handleSelectionChange('left')"
>
<el-table-column type="selection"></el-table-column>
<el-table-column prop="name" label="Name"></el-table-column>
<el-table-column prop="age" label="Age"></el-table-column>
<el-table-column prop="address" label="Address"></el-table-column>
</el-table>
</div>
<div class="table-transfer-buttons">
<el-button type="primary" icon="arrow-right" @click="transferRight"></el-button>
<el-button type="primary" icon="arrow-left" @click="transferLeft"></el-button>
</div>
<div class="table-transfer-right">
<el-table
ref="rightTable"
:data="rightList"
style="width: 100%"
@selection-change="handleSelectionChange('right')"
>
<el-table-column type="selection"></el-table-column>
<el-table-column prop="name" label="Name"></el-table-column>
<el-table-column prop="age" label="Age"></el-table-column>
<el-table-column prop="address" label="Address"></el-table-column>
</el-table>
</div>
</div>
</template>
<script>
export default {
data() {
return {
leftList: [],
rightList: []
};
},
methods: {
transferRight() {
const selection = this.$refs.leftTable.getSelection();
this.leftList = this.leftList.filter(item => !selection.includes(item));
this.rightList = this.rightList.concat(selection);
},
transferLeft() {
const selection = this.$refs.rightTable.getSelection();
this.rightList = this.rightList.filter(item => !selection.includes(item));
this.leftList = this.leftList.concat(selection);
},
handleSelectionChange(type) {
const table = type === 'left' ? this.$refs.leftTable : this.$refs.rightTable;
const selection = table.getSelection();
this.$set(this, type + 'List', selection);
}
}
};
</script>
<style scoped>
.table-transfer {
display: flex;
justify-content: space-between;
align-items: center;
}
.table-transfer-left {
width: 45%;
}
.table-transfer-buttons {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 10%;
}
.table-transfer-right {
width: 45%;
}
</style>
```
其中,`leftList` 和 `rightList` 分别代表左侧表格和右侧表格的数据,`transferRight` 和 `transferLeft` 分别代表将选中项从左侧移至右侧和从右侧移至左侧的方法,`handleSelectionChange` 方法用于监听选中项的变化。
3. 将穿梭框替换为表格穿梭框
最后,将原来的穿梭框替换为表格穿梭框即可。例如,将原来的代码:
```html
<el-transfer v-model="value" :data="list"></el-transfer>
```
替换为:
```html
<TableTransfer :leftList="tableData" :rightList="[]"></TableTransfer>
```
注意,这里的 `TableTransfer` 是前面创建的表格穿梭框组件。同时,传入的 `leftList` 应该是表格的数据,`rightList` 初始值为空数组。
阅读全文