el-table 选中/全选/反选/分页选中存储方法

🌰 html:

<el-table
  :data="table"
  ref="multipleTable"
  @select-all="selectAllChange"
  @select="selectChange"
  @row-click="handleCurrentChange"
>
  <el-table-column type="selection"></el-table-column>
  <el-table-column label="状态" align="left"></el-table-column>
</el-table>

🌰 js:

data(){
	return {
		table:[],
		selectedlist:[],//点击选中数据
	}
},
methods:{
	//checkbox点击时
	selectChange(selection, row) {
	  this.changeSelectData(row);
	},
	
	//每行点击时
	handleCurrentChange(row, event, column) {
	  this.changeSelectData(row);
	},
	
	//全选
	selectAllChange(selection) {
	  if (this.selectedlist.length == 0) {
	    this.selectedlist = selection.map(s=>s)
	  } else {
	    if (selection.length == 0) {
	      this.table.forEach(res => {
	        const index = this.selectedlist.findIndex(s=>s.id == res.id);
	        if (index > -1) {
	          this.selectedlist.splice(index, 1);
	        }
	      });
	    } else {
	      selection.forEach(res => {
	        const index = this.selectedlist.findIndex(s=>s.id == res.id);
	        if (index == -1) {
	            this.selectedlist.push(res);
	        }
	      });
	    }
	  }
	},
	
	// 选中存储方法
	changeSelectData(row) {
	  if (this.selectedlist.length > 0) {
	    let same = false;
	    const index = this.selectedlist.findIndex(s=>s.id == row.id);
	    if (index > -1) {
	      same = true;
	      this.selectedlist.splice(index,1)
	    } else {
	      this.selectedlist.push(row);
	    }
	    this.$refs.multipleTable.toggleRowSelection(row, !same);
	  } else {
	    this.$refs.multipleTable.toggleRowSelection(row, true);
	    this.selectedlist.push(row);
	  }
	},
	//刷新展示选中(分页请求数据,拿到数据后调用该方法)
	showSelected() {
	  this.$refs.multipleTable.$nextTick(_ => {
	    this.table.map(res => {
	      const index = this.selectedlist.findIndex(s=>s.id == res.id);
	      if (index > -1) {
	        this.$refs.multipleTable.toggleRowSelection(res, true);
	      }
	    });
	  });
	},
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tableElement UI 提供的表格组件,跨页全选指的是在表格分页情况下,实现全选功能。 要实现 el-table 的跨页全选,可以按照以下步骤进行操作: 1. 设置表格的 selection 属性为 true,开启选择模式: ```vue <el-table :data="tableData" :selection="true"> <!-- 表格列配置 --> </el-table> ``` 2. 定义一个变量 selectedRows 来保存选中的行数据: ```vue data() { return { tableData: [], // 表格数据源 selectedRows: [] // 选中的行数据 }; } ``` 3. 监听表格的 selection-change 事件,将选中/取消选中的行数据保存到 selectedRows 变量中: ```vue <el-table :data="tableData" :selection="true" @selection-change="handleSelectionChange"> <!-- 表格列配置 --> </el-table> methods: { handleSelectionChange(val) { this.selectedRows = val; } } ``` 4. 在表格的底部插槽中添加全选按钮,并绑定全选/取消全选方法: ```vue <template slot="footer"> <el-checkbox v-model="isAllSelected" @change="handleSelectAll"></el-checkbox> </template> data() { return { tableData: [], // 表格数据源 selectedRows: [], // 选中的行数据 isAllSelected: false // 是否全选 }; }, methods: { handleSelectAll() { if (this.isAllSelected) { this.selectedRows = [...this.tableData]; } else { this.selectedRows = []; } } } ``` 5. 判断当前页的所有行是否都被选中,更新全选按钮的状态: ```vue <template slot="footer"> <el-checkbox v-model="isAllSelected" @change="handleSelectAll"></el-checkbox> </template> methods: { handleSelectAll() { if (this.isAllSelected) { this.selectedRows = [...this.tableData]; } else { this.selectedRows = []; } }, // 监听表格数据的变化 tableDataChange(newData) { if (newData.length === 0) { this.isAllSelected = false; } else { this.isAllSelected = this.selectedRows.length === newData.length; } } }, watch: { tableData: { handler: 'tableDataChange', immediate: true } } ``` 通过以上步骤,你可以在 el-table 中实现跨页全选功能。当表格分页时,选中某一页的行数据后,可以点击全选按钮将当前页及其他已选中的页的行数据全部选中,取消全选则取消所有已选中的行数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值