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()
    }
}
相关推荐
verohh4 分钟前
第二篇:模块解耦与边界治理——清理长期迭代积累的模块耦合问题
前端
律宏阔8 分钟前
微信小程序 + Taro + TanStack Query 缓存与页面返回刷新踩坑实录
前端·微信小程序·taro
map_3d_vis2 小时前
百度地图 Hello World:5 分钟用 JSAPI 4.0 显示一张地图
前端·javascript·地图·百度地图
不要试图纠正别人2 小时前
构建 0 报错,产物体积翻倍:换引擎最容易漏的对账
前端·javascript
会编程的吕洞宾2 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤2 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha2 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
恋猫de小郭2 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
Hilaku2 小时前
面了 30 家公司拿到 2 个 Offer!
前端·javascript·程序员
延晗3 小时前
【从零开始 DeepSeek Harness 插件开发教程】三、从零开发 DeepSeek Harness 插件——完成第一个插件的挂载与运行
前端·人工智能