• el-table 多选回显,分页回显


    实现el-table多选分页回显功能,左侧是分页的数据源,右侧是选择后的人员数据,切换下一页,选中的数据会在左侧表格回显。

    实现:

    el-table的ref、row-key、select、select-all、type="selection"、:reserve-selection="true"都是需要设置的,并且表格绑定的data初始值不能为null,可以设置[]

    设置row-key

    1. getRowKeys(row) {
    2. return row.userId
    3. },

    表格选择数据,@select="handleSelectionChange" @select-all="selectAll"  选择单个和全选都要写

    1. handleSelectionChange(arr, row) {
    2. const bool = this.selectedUsers.some(user => user.userId === row.userId) // 存在返回true 否则返回false
    3. if (bool) {
    4. // 存在删除
    5. this.selectedUsers = this.selectedUsers.filter(user => user.userId !== row.userId)
    6. } else {
    7. // 添加
    8. this.selectedUsers.push(row)
    9. }
    10. },
    11. selectAll(arr){
    12. if (arr.length !== 0) {
    13. // 全选
    14. arr.forEach(item => {
    15. const bool = this.selectedUsers.some(user => user.userId === item.userId) // 存在返回true 否则返回false
    16. if (!bool) {
    17. // 不存在添加
    18. this.selectedUsers.push(item)
    19. }
    20. })
    21. } else {
    22. // 取消全选
    23. this.tableData.forEach(item => {
    24. this.selectedUsers = this.selectedUsers.filter(user => user.userId !== item.userId)
    25. })
    26. }
    27. },

    删除右侧选中数据的时候,不仅要对右侧选中数组处理,还要把左侧数组的选中状态设为未选中;

    1. delUser(node) {
    2. this.selectedUsers = this.selectedUsers.filter(user => user.userId !== node.userId)
    3. this.tableData.forEach(item => {
    4. if (node.userId === item.userId) {
    5. // 存在添加
    6. this.$refs.table.toggleRowSelection(item, false)
    7. }
    8. })
    9. },

    在数据编辑的时候,回显设置。注意切换table的page的时候要清除table的选中状态,重新设置选中状态,因为当右侧删除选中的数据不是当前页,分页切换的时候要刷新table的选中状态。

    1. queryList() {
    2. this.loading = true
    3. let queryParams = {
    4. pageNum: this.page.pageNum,
    5. pageSize: this.page.pageSize,
    6. }
    7. queryUserData(queryParams).then((res) => {
    8. if (res.code === 200) {
    9. this.tableData = res.rows
    10. this.total = res.total
    11. // 切换分页的时候要清楚table的选中状态,在根据selectedUsers的值设置table选中状态
    12. this.$refs.table.clearSelection()
    13. if (this.selectedUsers.length > 0) {
    14. this.$nextTick(()=>{
    15. for (let i = 0; i < this.tableData.length; i++) {
    16. this.selectedUsers.forEach(item=>{
    17. if (item.userId == this.tableData[i].userId){
    18. this.$refs.table.toggleRowSelection(this.tableData[i], true);
    19. }
    20. })
    21. }
    22. })
    23. }
    24. }
    25. })
    26. .finally(() => {
    27. this.loading = false
    28. })
    29. },

    css样式设置

  • 相关阅读:
    Python 中的线程
    如何查询淘宝天猫的宝贝类目
    Python itertools教程(python中的迭代器与组合迭代器)
    HTML进阶
    分享68个毕业答辩PPT,总有一款适合您
    Opengl之立方体贴图
    Vue项目打包部署到Gitee Pages配置教程(无空白bug)
    什么是函数式编程(functional programming)?在JavaScript中如何实现函数式编程的概念?
    TypeScript 快速入门之基础语法(一篇文章精通系列)(我的第一个TS程序【一】)【WebStorm版本】
    基于python的火车票售票系统/基于django火车票务网站/火车购票系统
  • 原文地址:https://blog.csdn.net/Bynine9/article/details/139676315