Vue+elementUI的el-table组件,合并单元格,勾选之后,复制、新增、删除、批量复制、批量删除功能
warning:
这篇文章距离上次修改已过205天,其中的内容可能已经有所变动。
在Vue中使用elementUI的el-table
组件进行合并单元格操作时,可以通过自定义列模板的方式实现。以下是一个简化的例子,展示了如何在使用el-table
时进行合并单元格,以及如何实现勾选后的复制、新增、删除和批量复制功能。
<template>
<div>
<el-table
:data="tableData"
style="width: 100%"
@selection-change="handleSelectionChange"
>
<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
label="操作">
<template slot-scope="scope">
<el-button @click="handleCopy(scope.$index, scope.row)">复制</el-button>
<el-button @click="handleDelete(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-button @click="handleBatchCopy">批量复制</el-button>
<el-button @click="handleAdd">新增</el-button>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2016-05-02',
name: '王小虎',
}, {
date: '2016-05-04',
name: '李小虎',
}],
multipleSelection: []
}
},
methods: {
handleSelectionChange(val) {
this.multipleSelection = val;
},
handleCopy(index, row) {
// 复制逻辑
this.tableData.push({ ...row });
},
handleDelete(index, row) {
// 删除逻辑
this.tableData.splice(index, 1);
},
handleBatchCopy() {
// 批量复制逻辑
this.multipleSelection.forEach(row => {
this.tableData.push({ ...row });
});
},
handleAdd() {
// 新增逻辑
const newRow = { date: '新日期', name: '新姓名' };
this.tableData.push(newRow);
}
}
}
</script>
在这个例子中,我们定义了一个表格,其中包含选择、日期、姓名和操作四个列。操作列包含了复制和删除按钮。使用@selection-change
事件来监听勾选项的变化,并将选中的行存储在multipleSelection
数组中。复制、删除和批量复制操作都是通过更新tableData
数组来实现的。这个例子提供了基本的功能实现,并且可以根据具体需求进行扩展和修改。
评论已关闭