官网文档:https://vxetable.cn

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

安装最新版 vxe-table

npm install vxe-pc-ui@4.2.50 vxe-table@4.8.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'
// ...

createApp(App).use(VxeUI).use(VxeUITable).mount('#app')
// ...

分页加载+虚拟滚动

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

{8CF56DD6-18A8-46DF-A67D-8DE66A302EE0}.png

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

<script setup>
import { reactive } from 'vue'

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)
  })
}

const gridOptions = reactive({
  border: true,
  height: 500,
  scrollY: {
    enabled: true,
    gt: 0
  },
  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 }
  ]
})
</script>

行的无限加载+虚拟滚动

原理:就是通过监听 scroll 滚动事件,当触底时加载下一页数据,滚动事件有很多有用的参数。
direction: 滚动方向
isTop:触碰顶部
isBottom:触碰底部
isLeft:触碰左侧
isRight:触碰右侧

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

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

<script setup>
import { reactive } from 'vue'

const gridOptions = reactive({
  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: []
})

let rowKey = 0
// 模拟行数据
const loadList = (size = 200) => {
  if (gridOptions.loading) {
    return
  }
  // 模拟后端接口
  gridOptions.loading = true
  setTimeout(() => {
    const dataList = []
    for (let i = 0; i < size; i++) {
      rowKey++
      dataList.push({
        id: `${rowKey}`,
        name: 'Test' + rowKey,
        role: 'Developer' + rowKey,
        sex: '男'
      })
    }
    gridOptions.data = [...gridOptions.data, ...dataList]
    gridOptions.loading = false
  }, 300)
}

const gridEvents = {
  scroll ({ isTop, isBottom }) {
    if (isTop) {
      console.log('触碰到顶部')
    }
    if (isBottom) {
      console.log('触碰到底部')
      loadList(20)
    }
  }
}

loadList(20)
</script>

列的无限加载+虚拟滚动

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

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

<script setup>
import { reactive } from 'vue'

const gridOptions = reactive({
  border: true,
  loading: false,
  showOverflow: true,
  showHeaderOverflow: true,
  showFooterOverflow: true,
  height: 600,
  columnConfig: {
    resizable: true
  },
  scrollX: {
    enabled: true,
    gt: 0
  },
  columns: [],
  data: []
})

let rowKey = 0
let colKey = 0
// 模拟行与列数据
const loadDataAndColumns = (rowSize, colSize) => {
  if (gridOptions.loading) {
    return
  }
  gridOptions.loading = true
  setTimeout(() => {
    const colList = []
    for (let i = 0; i < colSize; i++) {
      colKey++
      colList.push({
        field: `col${colKey}`,
        title: `标题${colKey}`,
        width: 160
      })
    }
    const columnList = [...gridOptions.columns, ...colList]
    const dataList = []
    for (let i = 0; i < rowSize; i++) {
      rowKey++
      const item = {
        id: `${rowKey}`
      }
      for (let j = 0; j < 10000; j++) {
        item[`col${j}`] = `值_${i}_${j}`
      }
      dataList.push(item)
    }
    gridOptions.columns = columnList
    gridOptions.data = [...gridOptions.data, ...dataList]
    gridOptions.loading = false
  }, 500)
}

const gridEvents = {
  scroll ({ isLeft, isRight }) {
    if (isLeft) {
      console.log('触碰到左侧')
    }
    if (isRight) {
      console.log('触碰到右侧')
      loadDataAndColumns(0, 15)
    }
  }
}

loadDataAndColumns(20, 15)
</script>

github https://github.com/x-extends/vxe-table
gitee


abc26296
172 声望24 粉丝