antdui a-table滚动加载

时间:25-09-24 栏目:Javascript 作者:zongyan86 评论:0 点击: 269 次

mounted

1
2
3
4
5
6
7
8
    //滚动监听事件
    this.$nextTick(() => {
      const tableComponent = this.$refs.myTable;
      if (tableComponent) {
        const tableContainer = tableComponent.$el.querySelector('.ant-table-body');
        tableContainer.addEventListener('scroll', this.handleScroll);
      }
    })

beforeUnmount

1
2
3
4
5
6
7
 this.$nextTick(() => {
      const tableComponent = this.$refs.myTable;
      if (tableComponent) {
        const tableContainer = tableComponent.$el.querySelector('.ant-table-body');
        tableContainer.removeEventListener('scroll', this.handleScroll);
      }
 })

method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
handleScroll(){
     const tableComponent = this.$refs.myTable;
     const tableContainer = tableComponent.$el.querySelector('.ant-table-body');
     const scrollPosition = tableContainer.scrollTop;
     const isAtTop = scrollPosition === 0;
     if (isAtTop) {
        //已滚动到顶部,执行相应操作
     }
     const isAtBottom = Math.abs(tableContainer.scrollHeight - scrollPosition - tableContainer.clientHeight)<1;
     if (isAtBottom) {
       // 已滚动到底部,执行相应操作
       this.fetchData({
          ...this.searchFormValues,
          page: ++this.currentPage
       });
     }
}

fetchData

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
fetchData(params) {
       // 模拟异步获取数据,实际应用中应替换为API调用
      this.loading = true;
      params.size=20;
      params.bimId =this.modelId;
      params.bimVersion=this.versionId;
      if(this.currentPage > this.totalPage) {
         // this.$message.warn('已经到底了...')
        this.tips="已经到底了..."
        this.isShowTips=true;
        let that=this;
        setTimeout(function(){
          that.isShowTips=false;
        },500)
        this.loading = false;
        return;
      }
      API.bimBillQuotaItemPage(params).then(data => {
        if (data.code === 0) {
          this.totalPage = data.data.totalPage;
          let newData = data.data.rows;
          this.data = [...this.data, ...newData];
          this.loading = false;
        } else {
          this.$message.error(data.msg);
          this.loading = false;
        }
      });
      // setTimeout(() => {
      //   const newData = Array.from({ length: this.pageSize }, (_, index) => ({
      //     key: this.data.length + index, // 生成唯一key值
      //     name: `Item ${this.data.length + index + 1}`, // 示例数据生成逻辑
      //     code: Math.floor(Math.random() * 100), // 示例数据生成逻辑
      //   }));
      //   this.data = [...this.data, ...newData]; // 追加新数据到数组中
      // }, 1000); // 模拟延迟,实际应用中应为网络请求时间
    },


声明: 本文由( zongyan86 )原创编译,转载请保留链接: antdui a-table滚动加载

关注我们