element table 手动全选记忆所勾选

实现效果如下

想法-----点击选中返回的为选中的数组其中selection-change在跳转页面时候触发,所以用了上两个方法

----》新建数组存放选中----》点击选中按钮返回其中对象----》遍历所存数组有此对象为删除,无为添加

其中需用

 this.tableData.forEach((row) => {

            this.$refs.tablebox.toggleRowSelection(row, true);

          });

来设置默认选中状态

核心代码如下

 handleSelectionChange(selection, row) {
      //判断点击的tag
      if (this.activeName === "2") {
        //倒序遍历删除所选内容
        for (let i = this.tableDataSlect.length - 1; i >= 0; i--) {
          if (this.tableDataSlect[i].id == row.id) {
            this.tableDataSlect.splice(i, 1);
          }
        }
        this.$nextTick(() => {
          this.tableData.forEach((row) => {
            this.$refs.tablebox.toggleRowSelection(row, true);
          });
        });
        this.tableData = this.tableDataSlect;
        this.tableDataLength = this.tableDataSlect.length;
      } else {
        // 判断为空直接传入
        if (this.tableDataSlect.length == 0) {
          this.tableDataSlect.push(row);
          this.tableDataLength = this.tableDataSlect.length;
          return false;
        }
        //   给定状态判断是否删除
        let stu = false;
        for (let i = this.tableDataSlect.length - 1; i >= 0; i--) {
          if (this.tableDataSlect[i].id == row.id) {
            this.tableDataSlect.splice(i, 1);
            stu = true;
          }
        }
        //   正序splice
        // for(let i=0;i<this.tableDataSlect.length;i++){
        // 	if(this.tableDataSlect[i].id==row.id){
        // 		console.log(i)
        // 		this.tableDataSlect.splice(i,1)
        // 		console.log(this.tableDataSlect,i)
        // 		 stu=true
        // 		 break;
        // 	}
        // }
        //   删除
        if (stu == false) {
          this.tableDataSlect.push(row);
        }
        this.tableDataLength = this.tableDataSlect.length;
      }
    },

全部实现放资源中,数据格式如下

 

另,,,所选中数组为本地分页                                      全部实现放资源中 ------》element 全选记忆

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Element UI Table 组件的 `header-cell-style` 属性和 `render-header` 事件来自定义表头的样式和行为。 首先,在 Table 组件中设置 `header-cell-style` 属性来为表头单元格添加一个复选框: ```html <el-table :data="tableData" header-cell-style="{ 'background-color': '#f5f7fa' }"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> </el-table> ``` 接下来,你需要在 `render-header` 事件中编写一个方法来监听表头的勾选事件。在该方法中,你可以获取到当前选中的列,并将其用于过滤数据: ```html <el-table :data="tableData" :header-cell-style="{ 'background-color': '#f5f7fa' }" @render-header="renderHeader"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> </el-table> ``` ```javascript methods: { renderHeader(h, { column }) { if (column.type === 'selection') { return ( <el-checkbox indeterminate={this.isIndeterminate} nativeOn-click={this.toggleAllSelection} value={this.isAllSelected} > 全选 </el-checkbox> ) } else { return column.label } }, toggleAllSelection(event) { if (event.target.checked) { this.$refs.table.toggleAllSelection() } else { this.$refs.table.clearSelection() } this.filterTable() }, filterTable() { const selectedColumns = this.$refs.table.store.states.selection.map(item => item.age) this.filteredTableData = this.tableData.filter(item => selectedColumns.indexOf(item.age) > -1) } } ``` 在上面的代码中,我们在 Table 组件中添加了一个 `render-header` 事件,并通过 `h()` 函数来渲染表头内容。在 `toggleAllSelection()` 方法中,我们通过 `$refs` 访问 Table 组件的实例,并调用 `toggleAllSelection()` 或 `clearSelection()` 方法来切换选中状态。最后,我们在 `filterTable()` 方法中使用 `selection` 数组中的列来过滤表格数据。 这样,当用户勾选表头中的复选框时,表格数据将会自动被过滤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值