vue表格滚动懒加载

直接上代码,粘贴即用。

		<el-table
          :data="tableData"
          class="tableList"
          border
          style="width: 100%"
          height="900"
        >
        </el-table>

  data() {
    return {
      tableData: [],
      Page: 1,//根据数据数量截取默认30条
      pageSize: 30,//进入页面默认截取数量
      allData:[],
    };
  },
  mounted() {
    this.handleScroll();
  },
methods: {
    //监听滚动条的是否滚动到元素底部,当滚动条滚动到元素底部时,进行数据加载事件的触发。
    //获取滚动元素的dom,进行滚动监听。
    handleScroll() {
      let that = this;
      const dom = document.getElementsByClassName("tableList")[0].children[2];
      dom.addEventListener("scroll", function () {
        const scrollDistance =
          dom.scrollHeight - dom.scrollTop - dom.clientHeight;
        if (scrollDistance <= 0) {
          that.initPage();
        }
      });
    },
    //每次page数+1,就进行数组的截取。table列表展示data的数据。
    initPage() {
      this.Page = this.Page + 1;
      this.tableData = this.allData.slice(0, this.Page * this.pageSize);
    },
    // 获取数据
    getData() {
        this.loading = true;
        getDetailreport({需要传递的参数}).then((res) => {
            if (res.code === 0) {
              this.loading = false;
              this.allData = res.data;
              this.tableData = this.allData.slice(0, this.pageSize);
            }
          })
          .catch((err) => {
            this.loading = false;
          });
    },
   }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Ant Design Vue 的子表格中,如果子表格数据比较多,可以使用懒加载来优化性能,只有当用户需要查看子表格时才去加载数据。 首先,在子表格的 columns 中添加一个 render 函数,并在该函数中渲染一个按钮,当用户点击该按钮时触发事件。 ```html <a-button type="link" @click="loadData(record)">查看详情</a-button> ``` 接着,在子组件中定义一个方法 loadData,该方法根据 record 中的 id 去请求子表格数据,并将数据存储在子组件的 data 中。 ```javascript loadData(record) { if (this.data[record.id]) { return; } axios.get('/api/subtable/' + record.id).then(response => { this.data[record.id] = response.data; this.$forceUpdate(); }); } ``` 注意,为了避免重复请求数据,我们在 data 中存储已经加载过的数据。 最后,在子表格的 render 函数中根据 data 中存储的数据来渲染子表格。 ```html <a-table :dataSource="data[record.id]" :columns="subColumns" /> ``` 完整的代码示例: ```html <template> <a-table :dataSource="dataSource" :columns="columns"> <template #expand="record"> <a-table :dataSource="data[record.id]" :columns="subColumns" /> <a-button type="link" @click="loadData(record)">查看详情</a-button> </template> </a-table> </template> <script> import axios from 'axios'; export default { data() { return { dataSource: [], columns: [ { title: 'ID', dataIndex: 'id', key: 'id', }, { title: 'Name', dataIndex: 'name', key: 'name', }, ], subColumns: [ { title: 'ID', dataIndex: 'id', key: 'id', }, { title: 'Description', dataIndex: 'description', key: 'description', }, ], data: {}, }; }, mounted() { axios.get('/api/table').then(response => { this.dataSource = response.data; }); }, methods: { loadData(record) { if (this.data[record.id]) { return; } axios.get('/api/subtable/' + record.id).then(response => { this.data[record.id] = response.data; this.$forceUpdate(); }); }, }, }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值