【element ui】table的使用:table自适应高度 && 选中多行数据,table再次渲染时回显多选的数据

本文介绍了如何在Element UI的Table组件中实现表格自适应高度和选中多行数据的功能。在自适应高度部分,通过双向绑定table的height属性和改变父元素高度来实现。在选中多行数据场景下,利用row-key和reserve-selection属性确保数据动态更新时,选中状态得到保留。示例代码和效果展示帮助理解整个实现过程。
摘要由CSDN通过智能技术生成

看门见山,直接上代码和截图!

table自适应高度 & reserve-selection的使用

table 自适应高度

  • 效果图
  1. 初始化页面布局
    在这里插入图片描述

  2. 全屏表格,1.中页面的基础上,点击向上 button
    在这里插入图片描述

  3. 2.中页面的基础上,点击向下button, 隐藏表格
    在这里插入图片描述

  • 实现

table表格自适应高度,主要利用table的 height属性

-template: 双向绑定table的height属性
在这里插入图片描述

    <
使用 element-plus 的 el-table 进行多选,可以通过绑定一个对象来实现数据回显。具体实现方法如下: 1. 在获取表格数据,将数据转化为一个以 id 为键,整个数据对象为值的字典对象。 2. 在获取回显数据,将数据转化为一个以 deviceId 为键,整个数据对象为值的字典对象。 3. 在 el-table 的 @select 事件中,将选中数据对象存入回显数据字典对象中。 4. 在 el-table 的 @select-all 事件中,遍历所有数据对象,将其存入回显数据字典对象中。 5. 在 el-table 的 :row-selected 事件中,根据回显数据字典对象中是否存在当前数据对象的 id 或 deviceId,来判断当前数据对象是否应该被选中。 具体代码实现可以参考以下示例: ``` <template> <el-table ref="tableRef" :data="tableData" @select="handleSelect" @select-all="handleSelectAll" :row-selected="isRowSelected" row-key="id" > <el-table-column type="selection" width="55" /> <el-table-column type="index" label="序号" width="150" /> <el-table-column prop="name" label="电厂名称" /> </el-table> </template> <script> export default { data() { return { tableData: [], // 表格数据 selectedData: {}, // 回显数据 } }, methods: { // 获取表格数据 async fetchData() { const res = await fetchTableData() this.tableData = res.data.reduce((dic, item) => { dic[item.id] = item return dic }, {}) }, // 获取回显数据 async fetchSelectedData() { const res = await fetchSelectedData() this.selectedData = res.data.reduce((dic, item) => { dic[item.deviceId] = item return dic }, {}) }, // 处理单个选中事件 handleSelect(selection, row) { this.$set(this.selectedData, row.deviceId, row) }, // 处理全选事件 handleSelectAll(selection) { Object.values(this.tableData).forEach(row => { this.$set(this.selectedData, row.deviceId, row) }) }, // 判断行是否被选中 isRowSelected(row) { return !!this.selectedData[row.id || row.deviceId] }, }, mounted() { this.fetchData() this.fetchSelectedData() }, } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值