在Vue中使用Element UI的el-table
组件实现多选列表的上移和下移功能,可以通过数组的splice
方法来移动数组中的元素。以下是一个简单的示例:
<template>
<div>
<el-button-group>
<el-button @click="moveUp">上移</el-button>
<el-button @click="moveDown">下移</el-button>
</el-button-group>
<el-table
:data="tableData"
style="width: 100%"
@selection-change="handleSelectionChange"
ref="multipleTable"
row-key="id"
>
<el-table-column type="selection" width="55"></el-table-column>
<el-table-column prop="date" label="日期" width="120"></el-table-column>
<el-table-column prop="name" label="姓名" width="120"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
// ... 填充您的数据
],
multipleSelection: [],
};
},
methods: {
handleSelectionChange(val) {
this.multipleSelection = val;
},
moveUp() {
const selection = this.multipleSelection;
if (selection.length === 0 || selection.some(item => this.tableData.indexOf(item) === 0)) return;
selection.forEach(item => {
const currentIndex = this.tableData.indexOf(item);
const targetIndex = currentIndex - 1;
this.tableData.splice(currentIndex, 1);
this.tableData.splice(targetIndex, 0, item);
});
},
moveDown() {
const selection = this.multipleSelection;
if (selection.length === 0 || selection.some(item => this.tableData.indexOf(item) === this.tableData.length - 1)) return;
selection.forEach(item => {
const currentIndex = this.tableData.indexOf(item);
const targetIndex = currentIndex + 1;
this.tableData.splice(currentIndex, 1);
this.tableData.splice(targetIndex, 0, item);
});
},
},
};
</script>
在这个示例中,moveUp
和moveDown
方法会根据当前选中的项来移动它们在tableData
数组中的位置。handleSelectionChange
方法用于更新选中项的数组。按钮点击时触发moveUp
或moveDown
方法。请确保tableData
中的每个对象都有一个唯一的id
属性,以便row-key
属性能够正确地识别每一行。