element ui的table选择遇到的坑(如翻页选中态消失)

产品需求实现两步的弹窗选择加table选中并展示到新的表格中,基本样式如下
在这里插入图片描述
在这里插入图片描述
1.在列表选择页选择数据展示到下面的新table中,但是翻页回来列表的选中态就取消了,根据ui文档的方法可以加上row-key

       <el-table //标签添加
         :row-key="getRowKey"
       ></el-table>
         //js
      getRowKey(row) {
      //缓存列表状态
      return row.id; //唯一表示,也可以直接跟着标签row-key后面写
    },
    //加上row-key不行的话还可以加上reserve-selection="true"
    <el-table-column
      type="selection"
      reserve-selection="true"
      width="55">
    </el-table-column>

2.点击下面table的取消选中上面的table的选中态也要跟着变,用ui文档上给的toggleRowSelection()方法在每次剩两个取消一个的时候就会出现列表状态不发生变化。解决方法如下,配合上面的方法就算翻页也可任意取消

    offCheck(id) {//multipleTable是el-table的ref,selection是上面table所有选中的数据,可以用他跟下面选中的table数据做比较
      //选中的表格取消事件
      this.$nextTick(() => {
        this.$refs.multipleTable.selection.forEach((ele) => {
          if (ele.id == id) {
            this.$refs.multipleTable.toggleRowSelection(ele, false); //指定元素取消选中:ele要取消选中的元素,fasle取消选中,true为选中
          }
        });
      });
    },

3.点击上一步回来,上面table的选中态失效,解决办法

        .then((res) => { //这是我上面table请求回来的数据
          if (res.error_code == 0) {
            this.add_gradeList = res.data.data;
            this.add_gradeList.forEach((ele)=>{   //checkoutTable是选中展示的table数据
             this.checkoutTable.forEach((item)=>{
                if(ele.id == item.id){
                   this.$refs.multipleTable.toggleRowSelection(ele, true);
                }
              })
            })
          }
        });

4可以根据以下方法判断点击的时候是true还是false

 let selected = selection.length && selection.indexOf(row) !== -1;
 //selected就是当前点击的选中态,ui的change事件第一个参数就是它
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI is a popular Vue.js UI library that provides a wide range of reusable and customizable UI components. One of these components is the table component, which allows developers to display data in a tabular format. To use the Element UI table component, you first need to install the Element UI library and import the necessary components in your project. Once you have done that, you can create a table by defining the columns and the data that you want to display. Here is an example of a basic Element UI table: ``` <template> <el-table :data="tableData"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 35, address: '123 Main St' }, { name: 'Jane', age: 28, address: '456 Elm St' }, { name: 'Bob', age: 42, address: '789 Oak St' } ] } } } </script> ``` In this example, we define a table with three columns: Name, Age, and Address. We also define an array of objects that represent the data that we want to display in the table. The `prop` attribute of each column specifies the key in the data object that corresponds to that column. There are many customization options available for the Element UI table component, such as sorting, filtering, pagination, and row selection. You can also use slots to customize the appearance of the table or add custom functionality.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值