ElementUI的表格设置勾选toggleRowSelection

文章目录


一、介绍

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

二、示例

1、使用

html 复制代码
<el-table ref="table" :data="tableData" @selection-change="handleSelectionChange">
</el-table>
js 复制代码
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)
                       } 
                    })
                })
                
                
            })    
        })
    }
}

2、分页切换的勾选消失

  • 切换下一页时,页面刷新tableData发生变化,在返回上一页,勾选的数据checkbox的check属性已经被重置为false,所以不会回显之前勾选的行
html 复制代码
<el-table ref="table" :data="tableData" @selection-change="handleSelectionChange">
</el-table>
js 复制代码
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()
    }
}
相关推荐
凤凰战士芭比Q9 分钟前
Nexus仓库(maven仓库、Yum仓库、APT仓库)
java·maven
全栈老石10 分钟前
从硬编码到 Schema 推断:前端表单开发的工程化转型
前端·vue.js·架构
weixin_4624462312 分钟前
【原创实践】使用 shell 脚本批量创建 Linux 用户并生成随机密码
linux·服务器·前端
864记忆24 分钟前
Linux操作系统自带的测试内存泄漏的命令
java·linux·运维
软件技术NINI36 分钟前
娃娃店html+css 4页
前端·css·html
Jul1en_37 分钟前
【算法】分治-归并类题目
java·算法·leetcode·排序算法
tryxr38 分钟前
volatile 的作用
java·jvm·volatile·指令重排序
wordbaby43 分钟前
TanStack Router 路径参数(Path Params)速查表
前端
独自归家的兔1 小时前
Java Robot 详解:系统级鼠标 / 键盘模拟的核心原理与实战
java·开发语言
梵尔纳多1 小时前
Electron 主进程和渲染进程通信
javascript·arcgis·electron