• ElementUI的表格设置勾选toggleRowSelection



    一、介绍

    //1、row:表格的行数据,选中/取消选中哪行,就是哪行的数据
        //注意:row只能是表格的data属性指定的数组中的数据,放其它数组中数据是无效的
    //2、selected:true-选中;false-取消选中
    this.$refs.table.toggleRowSelection(row, selected)
    
    • 1
    • 2
    • 3
    • 4
    • 该API一般在页面加载完成之后再调用
    • 该API会触发表格的selection-change事件,注意勾选数据的影响

    二、示例

    1、使用

    <el-table ref="table" :data="tableData" @selection-change="handleSelectionChange">
    el-table>
    
    • 1
    • 2
    data(){
        return{
            tableData: [],
            //选中的数据
            selectionTableData: []
        }
    },
    methods:{
        //表格选择项发生变化事件
        handleSelectionChange(selection){
            this.selectionTableData=selection
        },
        //表单修改
        handleUpdate(){
            getDetail().then(res=>{
                //表格数据赋值
                this.tableData=res.data.tableData
                //表格选中数据赋值
                this.selectionTableData=res.data.selectionTableData
                
                this.$nextTick(() => {
                    //设置表格勾选
                    //错误示例:使用非data属性指定的数组中的数据设置勾选
                    this.selectionTableData.forEach((item,index,arr)=>{
                        this.$refs.table.toggleRowSelection(item, true)
                    })
                    
                    //错误示例:由于toggleRowSelection方法会触发selection-change事件,直接使用this.selectionTableData循环,只要勾选一次,就会把this.selectionTableData=勾选的那一行数据,改变了要勾选的数据
                    this.tableData.forEach((dataItem,dataIndex,dataArr)=>{
                        this.selectionTableData.forEach((item,index,arr)=>{
                           if(JSON.stringify(dataItem)===JSON.stringify(item)){
                               this.$refs.table.toggleRowSelection(dataItem, true)
                           } 
                        })
                    })
                    
                    //正确示例
                    //选中表格数据的数组要赋值1个创建的新数组
                    const selectionArr=this.selectionTableData
                    this.tableData.forEach((dataItem,dataIndex,dataArr)=>{
                        this.selectionArr.forEach((item,index,arr)=>{
                           if(JSON.stringify(dataItem)===JSON.stringify(item)){
                              //放入API的row是表格data属性指定数组中的数据
                              this.$refs.table.toggleRowSelection(dataItem, true)
                           } 
                        })
                    })
                    
                    
                })    
            })
        }
    }
    
    • 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

    2、分页切换的勾选消失

    • 切换下一页时,页面刷新tableData发生变化,在返回上一页,勾选的数据checkbox的check属性已经被重置为false,所以不会回显之前勾选的行
    <el-table ref="table" :data="tableData" @selection-change="handleSelectionChange">
    el-table>
    
    • 1
    • 2
    data(){
        return{
            loading: true,
            total: 0,
            //表格数据
            tableData: [],
            //当前页勾选行数据
            currCheckData:[],
            //总勾选数据
            totalCheckData:[]
        }
    },
    methods:{
        //将本页勾选的行数据添加到总勾选数据里面
        addCurrPageData(){
            if(this.currCheckData.length!==0){
                this.totalCheckData=this.totalCheckData.concat(this.currCheckData)
                //添加进去之后清空本页勾选行数据
                this.currCheckData=[]
            }
        },
        //切换页会重新调用list  
        getList() {
            this.loading=true
            //切换页时,保存本页勾选行数据
            this.addCurrPageData()
            
            //请求后台下一页数据
            listQuestion(this.queryParams).then(res=>{
                this.tableData=res.rows
                this.total=res.total
                //本页之前可能勾选过,设置本页勾选行
                this.$nextTick(()=>{
                    this.tableData.forEach((dataItem,dataIndex,dataArr)=>{
                        this.totalCheckData.forEach((item,index,arr)=>{
                            if(dataItem.qustId===item.qustId){
                                //不需要装填本页之前勾选行数据,因为toggleRowSelection会触发表格的selection-change事件
                                //this.currCheckData.push(dataItem)
                                
                                //设置勾选状态
                                this.$refs.table.toggleRowSelection(dataItem,true)
            
                                //总勾选行中删除本页勾选行
                                this.totalCheckData.splice(index,1)
                            }
                        })
                    })
                })
                this.loading=false
            })
        },
        //表格勾选状态改变事件
        handleSelectionChange(selection){
            this.currCheckData=selection
        },
        //确定按钮
        checkQust(){
            //确定时,保存一次当前页勾选行
            this.addCurrPageData()
        }
    }
    
    • 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
  • 相关阅读:
    艾伦脑科学研究所,脑图谱,小鼠不同的功能脑区,可视化展示
    Redis概述及安装使用
    web前端课程设计——重庆旅游7页 HTML+CSS+JavaScript
    Windows系统解压zip文件之后乱码的问题的原因和解决方法
    Sentinel-流量防卫兵
    Vue中的方法和事件绑定
    Jmeter 自动化性能测试常见问题汇总
    基于SSM的网上书城系统设计与实现
    计算机毕业设计之java+ssm尤文图斯足球俱乐部网上商城系统
    SecureCRT之Xmodem操作步骤
  • 原文地址:https://blog.csdn.net/weixin_43476020/article/details/132713353