elementUI table分页点击后如何保持滚动条不变
时间: 2024-02-20 12:57:16 浏览: 483
要保持滚动条位置不变,可以监听分页器的 `current-change` 事件,在事件处理函数中获取当前滚动条的位置,并将其保存下来。然后在下一页加载完毕后,再将滚动条位置设置为之前保存的位置即可。
示例代码如下:
```html
<template>
<div>
<el-table :data="tableData" height="400" style="width: 100%" @sort-change="handleSortChange">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
<el-pagination
@current-change="handlePageChange"
:current-page="currentPage"
:page-sizes="[10, 20, 30, 40]"
:page-size="pageSize"
:total="total">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [],
currentPage: 1,
pageSize: 10,
total: 100,
scrollTop: 0, // 保存滚动条位置
};
},
methods: {
fetchData() {
// 发送请求获取数据
// ...
// 在请求完成后设置滚动条位置
this.$nextTick(() => {
this.$refs.tableRef.$el.querySelector('.el-table__body-wrapper').scrollTop = this.scrollTop;
});
},
handlePageChange(currentPage) {
this.scrollTop = this.$refs.tableRef.$el.querySelector('.el-table__body-wrapper').scrollTop;
this.currentPage = currentPage;
this.fetchData();
},
handleSortChange(sort) {
console.log(sort);
},
},
mounted() {
this.fetchData();
},
};
</script>
```
在上述代码中,`handlePageChange` 方法中保存了当前滚动条位置到 `scrollTop` 变量中,并在请求数据完成后重新设置滚动条位置。`$nextTick` 方法会在下一次 DOM 更新后执行回调函数,确保在滚动条位置设置之前,表格已经完成了渲染。同时,通过 `$refs` 属性获取到表格实例,然后通过 `querySelector` 方法获取到表格主体区域的 DOM 元素,从而获取和设置滚动条位置。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)