官網文件: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')
// ...
分頁載入+虛擬滾動
原理:透過點選分頁載入當前頁資料
<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