后台管理系统中的列表页面,通常都会有对列表数据进行批量操做的功能,例如:批量删除、批量删除等。
以前项目中只是简单的用到Element框架中常规的属性、事件。在一次机缘巧合下,了解到一个公司内部的框架是基于Element框架内部实现了一些插件功能,对于表格这一块完善了不少功能,当时没有把握住机会去看源码是怎么实现的,如今有点小后悔呢,嘤嘤嘤~~~~不要紧,本身慢慢一点一点实现。前端
事件代码:bash
getRowKeys (row) {
return row.execId
}复制代码
这样经过selectionChange方法就能获取页面中选中数据的改变,将选中的数据保存到list中框架
selectionChange (rows) {
this.checkList = rows
}复制代码
二、全选全部数据ui
element框架中有select-all事件,全选本页全部数据,可是项目中,常常会遇到说对全部的进行操做,例如批量删除(删除全部数据,这个权限有点大)
实现思路:
this
<el-checkbox v-model="allCheck" @change="allCheckEvent">全选全部</el-checkbox>复制代码
allCheckEvent () {
if (this.allCheck) {
this.testList.forEach(row => {
this.$refs.recordTable.toggleRowSelection(row, true)
})
} else {
this.$refs.recordTable.clearSelection()
}
}复制代码
watch: {
testList: {
handler (value) {
if (this.allCheck) {
let that = this
let len = that.checkList.length
value.forEach(row => {
for (let i = 0; i < len; i++) {
if (row.execId === that.checkList[i].execId) {
that.$refs.recordTable.toggleRowSelection(row, false)
break
} else {
that.$refs.recordTable.toggleRowSelection(row, true)
}
}
})
}
},
deep: true
}
}复制代码
selectOne () {
if (this.allCheck) {
this.allCheck = false
}
}复制代码
实现的表格:spa
走了很多弯路才注意到的问题:插件