使用 ElementUI 实现一个可以多选的表格,并且分页时不会丢失已选中的数据。并且支持根据保存的数据自动勾选。
主要用到 table 组件中的 reserve-selection
属性
具体代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <template> <div> <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" row-key="name" style="width: 100%" @selection-change="handleSelectionChange">
<el-table-column :reserve-selection="true" type="selection" align="center" width="55"> </el-table-column>
<el-table-column label="日期" width="120"> <template slot-scope="scope">{{ scope.row.date }}</template> </el-table-column> <el-table-column prop="name" label="姓名" width="120"> </el-table-column> <el-table-column prop="address" label="地址" show-overflow-tooltip> </el-table-column> </el-table> <el-pagination :page-size="2" layout="prev, pager, next" :total="4" @current-change="handleCurrentChange"> </el-pagination> </div> </template>
<script> export default { data() { return { pageNum: 1, tableData: [], alltableData: [ { date: '2016-05-02', name: '王小虎1', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-02', name: '王小虎2', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-02', name: '王小虎3', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-02', name: '王小虎4', address: '上海市普陀区金沙江路 1518 弄' } ], checkData: [ { date: '2016-05-02', name: '王小虎1', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-02', name: '王小虎4', address: '上海市普陀区金沙江路 1518 弄' } ] } }, mounted() { this.getData() this.checkData.forEach(item => { this.$refs.multipleTable.toggleRowSelection(item, true) }) }, methods: { getData() { this.tableData = this.alltableData.slice((this.pageNum - 1) * 2, this.pageNum * 2) }, handleSelectionChange(list) { console.log(list) }, handleCurrentChange(e) { console.log(e) this.pageNum = e this.getData() } } } </script>
|
演示效果
