element plus table配合无限滚动加载

文章展示了如何在Vue项目中安装和使用el-table-infinite-scroll插件实现无限滚动加载数据。首先通过npm安装插件,然后在组件中局部引用并定义指令。在JavaScript中设置加载更多数据的逻辑,同时从API获取数据并进行分页处理。HTML部分展示了el-table的配置,包括加载事件和分页参数。
摘要由CSDN通过智能技术生成

1、安装插件

npm install --save el-table-infinite-scroll

2、局部引用

import elTableInfiniteScroll from 'el-table-infinite-scroll';

3、js

  directives: {
      'el-table-infinite-scroll': elTableInfiniteScroll,
    },
    setup() {}
    //加载更多
      const loadResult = () => {
        if (state.count < state.resultListTotal.length) {
          state.count += 5
        } else {
          state.count = state.resultListTotal.length
          state.isLoad = true
        }

        state.resultList = state.resultListTotal.slice(0, state.count)
      }
      //获取发票批及配单结果
      const matchInvoiceBatchFun = async () => {
        try {
          state.resultLoading = true
          const data = await $matchInvoiceBatch({
            id: route.query.id,
            flag: 'CMS',
          })

          state.resultLoading = false
          state.batchForm = data.data
          state.invoiceBatchId = data.data.id
          state.resultListTotal = data.data.invoiceBatchMatchResult
          state.resultList = state.resultListTotal.slice(0, 5)
          state.expandRowKeys = []
          state.resultListTotal?.forEach((i) => {
            state.expandRowKeys.push(i.invoiceId)
          })

          state.invoiceHeaderData = JSON.parse(
            sessionStorage.getItem('invoiceHeaderData')
          )
        } catch (error) {
          state.resultLoading = false
        }
      }

4、html

     <el-table
          ref="tableSortRef"
          v-loading="resultLoading"
          :border="border"
          :cell-class-name="orderRowClassName3"
          class-name="resultTableAll"
          :data="resultList"
      
          @selection-change="resultSelectChange"
          class="infinite-list"
          v-el-table-infinite-scroll="loadResult"
          :infinite-scroll-disabled="isLoad"
          :infinite-scroll-immediate="isLoad"
        >
          <el-table-column align="center" type="selection" width="30" />
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-table 无限滚动是指在表格中滚动时,可以动态加载更多的数据,而不是一次性加载所有数据。这种方式可以提高表格的性能和用户体验。下面是使用 element-ui 和 el-table-infinite-scroll 插件实现 el-table 无限滚动的步骤: 1. 安装 el-table-infinite-scroll 插件: ```bash npm install --save el-table-infinite-scroll ``` 2. 在需要使用无限滚动的 el-table 组件上添加 v-el-table-infinite-scroll 指令,并绑定一个方法,该方法会在滚动到底部时被调用: ```html <template> <el-table v-el-table-infinite-scroll="loadMoreData" :data="tableData" height="300"> <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> </template> <script> import ElTableInfiniteScroll from 'el-table-infinite-scroll'; export default { directives: { ElTableInfiniteScroll }, data() { return { tableData: [], currentPage: 1, pageSize: 10, }; }, mounted() { this.loadMoreData(); }, methods: { loadMoreData() { // 模拟异步加载数据 setTimeout(() => { const newData = []; for (let i = 0; i < this.pageSize; i++) { const index = (this.currentPage - 1) * this.pageSize + i; newData.push({ date: `2016-05-${index + 1}`, name: `王小虎${index}`, address: `上海市普陀区金沙江路 ${index} 弄`, }); } this.tableData = [...this.tableData, ...newData]; this.currentPage++; }, 1000); }, }, }; </script> ``` 在上面的例子中,loadMoreData 方法会模拟异步加载数据,并将新数据添加到表格数据中。每次加载数据时,currentPage 会自增,以便下次加载时获取正确的数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值