表格滚动分页查询

1.给表格添加id,height="100%"是固定表头

<el-table

            id="attr-table-box"

            height="100%"

            :row-style="{ height: '66px' }"

            style="width: 100%; height: 100%; "

          >

2.使用计算属性获取总页数

 computed: {

    //  表格总页数,控制滚动查询 attrTotal是数据总数;

    attrTotalPage() {

      return Math.ceil(this.attrTotal / this.queryParamsAttr.pageSize);

    },

  },

3.主要方法
// 属性表格绑定滚动事件,滚动分页查询
    queryByScroll() {
      const _this = this;
      const tableScrollDom =
        document.getElementById("attr-table-box").childNodes[2];
      tableScrollDom.addEventListener("scroll", function () {
        const scrollTop = tableScrollDom.scrollTop;
        const clientHeight = tableScrollDom.clientHeight;
        const scrollHeight = tableScrollDom.scrollHeight;
        if (scrollTop + clientHeight == scrollHeight) {
          // 滚动到底部
          if (_this.attrTotalPage > _this.queryParamsAttr.pageNum) {
            // 如果当前还没到最后一页,滚动到底部以后继续查询
            _this.queryParamsAttr.pageNum++;
            // 请求数据
            _this.getAttrData();
          }
        } else {
        }
      });
    },
4.在mounted里调用

 mounted() {

     this.queryByScroll();

  },

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 表格组件提供了分页功能,但默认情况下只显示一页数据。如果你想要实现滚动分页,可以通过以下步骤来实现: 1. 在表格组件中添加 `scroll` 属性来启用滚动条。 2. 将 `pagination` 属性设置为 `false`,以禁用默认的分页。 3. 使用 `@scroll` 事件监听表格滚动事件。 4. 在滚动事件处理函数中,根据滚动条位置和表格高度计算出当前页码。 5. 根据当前页码,使用 `dataSource` 属性来更新表格数据。 具体实现可参考以下代码示例: ```html <template> <a-table :columns="columns" :dataSource="dataSource" :scroll="{ y: 400 }" :pagination="false" @scroll="handleScroll"> <template slot="name" slot-scope="text">{{ text }}</template> </a-table> </template> <script> import { Table } from 'ant-design-vue'; export default { components: { 'a-table': Table, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name', width: 150, }, { title: 'Age', dataIndex: 'age', key: 'age', width: 70, }, { title: 'Address', dataIndex: 'address', key: 'address', width: 200, }, ], dataSource: [], currentPage: 1, }; }, mounted() { this.loadData(); }, methods: { loadData() { // 根据当前页码加载数据 const pageSize = 10; const start = (this.currentPage - 1) * pageSize; const end = start + pageSize; const data = []; for (let i = start; i < end; i++) { data.push({ key: i, name: `User ${i}`, age: Math.floor(Math.random() * 30) + 20, address: `Street ${i}`, }); } this.dataSource = data; }, handleScroll(e) { const tableBody = e.target.querySelector('.ant-table-body'); // 计算当前页码 const currentPage = Math.floor(tableBody.scrollTop / tableBody.clientHeight) + 1; if (currentPage !== this.currentPage) { this.currentPage = currentPage; this.loadData(); } }, }, }; </script> ``` 在以上代码中,我们设置表格高度为 400px,并禁用了默认的分页。当表格滚动时,会触发 `handleScroll` 方法来更新当前页码并加载数据。注意,为了方便起见,这里的数据是随机生成的,实际应用中需要根据实际情况来获取数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值