<el-table border :data="infoDataList" max-height="300" :row-class-name="tableRowClassName">不使用插件,如何使只加载可视部分
时间: 2023-07-06 09:38:47 浏览: 46
好的,你的问题是如何不使用插件实现只加载可视部分的`<el-table>`表格。在Vue.js中,你可以使用`v-show`指令和计算属性来实现这一功能。以下是实现方法:
1. 在你的Vue.js组件中,定义一个计算属性来计算当前可见的行数。例如:
```javascript
computed: {
visibleRowCount() {
const table = this.$refs['my-table'].$el.querySelector('.el-table__body-wrapper');
const rowHeight = 40; // 每行的高度
const visibleHeight = table.clientHeight; // 可见区域的高度
return Math.ceil(visibleHeight / rowHeight);
}
}
```
这个计算属性会计算可见区域内的行数,假设每行高度为40像素。
2. 使用`v-show`指令来控制每行的可见性。例如:
```html
<template>
<el-table ref="my-table" border max-height="300" :row-class-name="tableRowClassName">
<el-table-column
v-for="(column, index) in columns"
:key="index"
:prop="column.prop"
:label="column.label"
>
</el-table-column>
<el-table-column
fixed="right"
label="操作"
width="100"
>
<template slot-scope="scope">
<el-button type="text" size="small">编辑</el-button>
<el-button type="text" size="small">删除</el-button>
</template>
</el-table-column>
<el-table-body>
<el-table-row
v-for="(row, index) in infoDataList"
:key="index"
v-show="index >= startIndex && index < endIndex"
>
<el-table-column
v-for="(column, index) in columns"
:key="index"
:prop="column.prop"
>
{{ row[column.prop] }}
</el-table-column>
</el-table-row>
</el-table-body>
</el-table>
</template>
```
在上面的代码中,我们使用`v-show`指令来控制每行的可见性。`startIndex`和`endIndex`是计算属性`visibleRowCount`计算出的可见行的起始和结束索引。只有在这个范围内的行才会被渲染和显示。
通过这种方式,`<el-table>`表格将只渲染可见的部分,同时保持高性能。希望这个方法对你有所帮助!