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()
    }
}
相关推荐
code2cat6 小时前
Java工具篇之Maven:从pom.xml到测试通过的JAR
xml·java·maven
wuminyu12 小时前
JVM规范学习总结:解释器与JIT编译器如何处理JVM字节码
java·linux·c语言·jvm·c++
txxzjmzlh12 小时前
SpringBoot 配置⽂件
java·spring boot·spring
Wang's Blog13 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象32713 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
独泪了无痕13 小时前
Hutool之LocalDateTimeUtil:让时间间隔操作如虎添翼
java
花开盛夏^.^14 小时前
电脑打开多个浏览器Web窗口占用较大的运行内存的原因
前端·浏览器
Dxy123931021614 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷14 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
馒头不想说话15 小时前
实体店老板的24小时AI运营员工,写、剪、发全自动,省下一整个团队工资
前端