vxe-table v3.10+ 实现无限加载数据,虚拟滚动+无限滚动分页加载

1,075 阅读3分钟

官网文档:vxetable.cn

对于表格列表分页,vxe-table 支持多种方式。
第一种就是普通使用分页组件配合虚拟滚动单页一次性加载大量数据。
第二钟就是使用无限加载配合虚拟滚动,实现不分页效果加载全表数据,就是用户看起来没分页,实际是通过滚动到底部触发加载下一页数据来达到看起来不分页的的效果。

安装最新版

npm install vxe-pc-ui@3.2.18 vxe-table@3.10.10
// ...
import VxeUI from 'vxe-pc-ui'
import 'vxe-pc-ui/lib/style.css'
import VxeUITable from 'vxe-table'
import 'vxe-table/lib/style.css'
// ...

Vue.use(VxeUI)
Vue.use(VxeUITable)
// ...

分页加载+虚拟滚动

原理:通过点击分页加载当前页数据

{3075BAC4-3F08-4106-B141-BFE4D04362BD}.png

<template>
  <div>
    <vxe-grid v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script>
const list = [
  { id: 10001, name: 'Test1', nickname: 'T1', role: 'Develop', sex: 'Man', age: 28, address: 'Shenzhen' },
  { id: 10002, name: 'Test2', nickname: 'T2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
  { id: 10003, name: 'Test3', nickname: 'T3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
  { id: 10004, name: 'Test4', nickname: 'T4', role: 'Designer', sex: 'Women', age: 23, address: 'test abc' },
  { id: 10005, name: 'Test5', nickname: 'T5', role: 'Develop', sex: 'Women', age: 30, address: 'Shanghai' },
  { id: 10006, name: 'Test6', nickname: 'T6', role: 'Designer', sex: 'Women', age: 21, address: 'Shenzhen' },
  { id: 10007, name: 'Test7', nickname: 'T7', role: 'Test', sex: 'Man', age: 29, address: 'Shenzhen' },
  { id: 10008, name: 'Test8', nickname: 'T8', role: 'Develop', sex: 'Man', age: 35, address: 'test abc' },
  { id: 10009, name: 'Test9', nickname: 'T9', role: 'Develop', sex: 'Man', age: 35, address: 'Shenzhen' },
  { id: 100010, name: 'Test10', nickname: 'T10', role: 'Develop', sex: 'Man', age: 35, address: 'Guangzhou' },
  { id: 100011, name: 'Test11', nickname: 'T11', role: 'Develop', sex: 'Man', age: 49, address: 'Guangzhou' },
  { id: 100012, name: 'Test12', nickname: 'T12', role: 'Develop', sex: 'Women', age: 45, address: 'Shanghai' },
  { id: 100013, name: 'Test13', nickname: 'T13', role: 'Test', sex: 'Women', age: 35, address: 'Guangzhou' },
  { id: 100014, name: 'Test14', nickname: 'T14', role: 'Test', sex: 'Man', age: 29, address: 'Shanghai' },
  { id: 100015, name: 'Test15', nickname: 'T15', role: 'Develop', sex: 'Man', age: 39, address: 'Guangzhou' },
  { id: 100016, name: 'Test16', nickname: 'T16', role: 'Test', sex: 'Women', age: 35, address: 'Guangzhou' },
  { id: 100017, name: 'Test17', nickname: 'T17', role: 'Test', sex: 'Man', age: 39, address: 'Shanghai' },
  { id: 100018, name: 'Test18', nickname: 'T18', role: 'Develop', sex: 'Man', age: 44, address: 'Guangzhou' },
  { id: 100019, name: 'Test19', nickname: 'T19', role: 'Develop', sex: 'Man', age: 39, address: 'Guangzhou' },
  { id: 100020, name: 'Test20', nickname: 'T20', role: 'Test', sex: 'Women', age: 35, address: 'Guangzhou' },
  { id: 100021, name: 'Test21', nickname: 'T21', role: 'Test', sex: 'Man', age: 39, address: 'Shanghai' },
  { id: 100022, name: 'Test22', nickname: 'T22', role: 'Develop', sex: 'Man', age: 44, address: 'Guangzhou' }
]
// 模拟接口
const findPageList = (pageSize, currentPage) => {
  console.log(`调用查询接口 pageSize=${pageSize} currentPage=${currentPage}`)
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        result: list.slice((currentPage - 1) * pageSize, currentPage * pageSize),
        page: {
          total: list.length
        }
      })
    }, 100)
  })
}
export default {
  data () {
    const gridOptions = {
      border: true,
      height: 500,
      pagerConfig: {},
      proxyConfig: {
        // props: {
        //   result: 'result', // 配置响应结果列表字段
        //   total: 'page.total' // 配置响应结果总页数字段
        // },
        ajax: {
          query: ({ page }) => {
            // 默认接收 Promise<{ result: [], page: { total: 100 } }>
            return findPageList(page.pageSize, page.currentPage)
          }
        }
      },
      columns: [
        { type: 'seq', width: 70 },
        { field: 'name', title: 'Name' },
        { field: 'nickname', title: 'Nickname' },
        { field: 'role', title: 'Role' },
        { field: 'address', title: 'Address', showOverflow: true }
      ]
    }
    return {
      gridOptions
    }
  }
}
</script>

行的无限加载+虚拟滚动

原理:就是通过滚动事件,当触底时加载下一页数据

Video_2024-11-11_101252-ezgif.com-video-to-gif-converter.gif

这种方式既能非常流畅的懒加载数据,又能使用虚拟滚动的高性能渲染。实现用户看起来不分页,又能丝滑渲染大量数据的效果。

<template>
  <div>
    <vxe-grid v-bind="gridOptions" @scroll="scrollEvent"></vxe-grid>
  </div>
</template>

<script>
export default {
  data () {
    const gridOptions = {
      border: true,
      loading: false,
      showOverflow: true,
      showHeaderOverflow: true,
      showFooterOverflow: true,
      height: 600,
      columnConfig: {
        resizable: true
      },
      scrollY: {
        enabled: true,
        gt: 0
      },
      columns: [
        { type: 'seq', width: 80 },
        { field: 'id', title: 'ID', width: 120 },
        { field: 'name', title: 'Name' },
        { field: 'role', title: 'Role' },
        { field: 'sex', title: 'Sex', width: 100 }
      ],
      data: []
    }
    return {
      gridOptions,
      rowKey: 0
    }
  },
  methods: {
    // 模拟行数据
    loadList (size = 200) {
      if (this.gridOptions.loading) {
        return
      }
      // 模拟后端接口
      this.gridOptions.loading = true
      setTimeout(() => {
        const dataList = []
        for (let i = 0; i < size; i++) {
          this.rowKey++
          dataList.push({
            id: `${this.rowKey}`,
            name: 'Test' + this.rowKey,
            role: 'Developer' + this.rowKey,
            sex: '男'
          })
        }
        this.gridOptions.data = [...this.gridOptions.data, ...dataList]
        this.gridOptions.loading = false
      }, 300)
    },
    scrollEvent ({ isTop, isBottom }) {
      if (isTop) {
        console.log('触碰到顶部')
      }
      if (isBottom) {
        console.log('触碰到底部')
        this.loadList(20)
      }
    }
  },
  created () {
    this.loadList(20)
  }
}
</script>

列的无限加载+虚拟滚动

原理和行是一样的,也是滚动触底就加载下一页数据

Video_2024-11-11_102001-ezgif.com-video-to-gif-converter.gif

<template>
  <div>
    <vxe-grid v-bind="gridOptions" @scroll="scrollEvent"></vxe-grid>
  </div>
</template>

<script>
export default {
  data () {
    const gridOptions = {
      border: true,
      loading: false,
      showOverflow: true,
      showHeaderOverflow: true,
      showFooterOverflow: true,
      height: 600,
      columnConfig: {
        resizable: true
      },
      scrollX: {
        enabled: true,
        gt: 0
      },
      columns: [],
      data: []
    }
    return {
      gridOptions,
      rowKey: 0,
      colKey: 0
    }
  },
  methods: {
    // 模拟行与列数据
    loadDataAndColumns (rowSize, colSize) {
      if (this.gridOptions.loading) {
        return
      }
      this.gridOptions.loading = true
      setTimeout(() => {
        const colList = []
        for (let i = 0; i < colSize; i++) {
          this.colKey++
          colList.push({
            field: `col${this.colKey}`,
            title: `标题${this.colKey}`,
            width: 160
          })
        }
        const columnList = [...this.gridOptions.columns, ...colList]
        const dataList = []
        for (let i = 0; i < rowSize; i++) {
          this.rowKey++
          const item = {
            id: `${this.rowKey}`
          }
          for (let j = 0; j < 10000; j++) {
            item[`col${j}`] = `值_${i}_${j}`
          }
          dataList.push(item)
        }
        this.gridOptions.columns = columnList
        this.gridOptions.data = [...this.gridOptions.data, ...dataList]
        this.gridOptions.loading = false
      }, 500)
    },
    scrollEvent ({ isLeft, isRight }) {
      if (isLeft) {
        console.log('触碰到左侧')
      }
      if (isRight) {
        console.log('触碰到右侧')
        this.loadDataAndColumns(0, 15)
      }
    }
  },
  created () {
    this.loadDataAndColumns(20, 15)
  }
}
</script>

github
gitee