ExtJS 數據網格的分頁是企業級應用核心功能。本文詳細講解 Ext.data.Store 分頁配置、PagingToolbar 整合、後端標準協議參數(page/start/limit/sort/filter)、遠程排序過濾、緩存策略、大數據緩衝渲染。
Ext.define('App.store.UserStore', {
extend: 'Ext.data.Store',
alias: 'store.user',
model: 'App.model.User',
pageSize: 50, // 每頁條數
proxy: {
type: 'ajax',
url: '/api/users',
reader: {
type: 'json',
rootProperty: 'data', // 數據數組
totalProperty: 'total', // 總條數
successProperty: 'success' // 可選
},
// 參數名映射(可選,符合後端約定)
startParam: 'offset', // 默認 start
limitParam: 'limit', // 默認 limit
pageParam: 'page', // 默認 page
sortParam: 'sort', // 排序參數
filterParam: 'filter', // 過濾參數
groupParam: 'group', // 分組參數
directionParam: 'dir' // 排序方向
},
// 遠程排序/過濾(不在本地排序)
remoteSort: true,
remoteFilter: true,
// 緩存頁面:用戶翻頁返回時無需重新請求
// buffered: true, // 7.0+ 現代緩衝存儲
// leadingBufferZone: 100, trailingBufferZone: 100
autoLoad: true
});
Ext.create('Ext.grid.Panel', {
title: '用戶管理',
store: 'user',
columns: [
{ text: 'ID', dataIndex: 'id', width: 60 },
{ text: '姓名', dataIndex: 'name', flex: 1 },
{ text: '郵箱', dataIndex: 'email', flex: 1 }
],
dockedItems: [{
xtype: 'pagingtoolbar',
dock: 'bottom',
store: 'user',
displayInfo: true,
displayMsg: '顯示 {0} - {1} 共 {2} 條',
emptyMsg: '無數據',
beforePageText: '第',
afterPageText: '頁,共 {0} 頁'
}],
// 頂部工具欄:刷新、每頁條數選擇
tbar: [
{ xtype: 'button', text: '刷新', handler: 'onRefresh' },
'->',
{ xtype: 'combobox', width: 80, value: 50, store: [25,50,100,200],
listeners: { select: (c, r) => c.up('grid').store.setPageSize(r[0].data.field1) }
}
]
});
// 請求參數示例
// GET /api/users?offset=0&limit=50&sort=[{"property":"name","direction":"ASC"}]&filter=[{"property":"status","value":"active"}]
// 響應
{
"success": true,
"total": 1234,
"data": [
{ "id": 1, "name": "張三", "email": "zhang@example.com" },
...
]
}
@GetMapping("/users")
public PageResult list(
@RequestParam(defaultValue = "0") int offset,
@RequestParam(defaultValue = "50") int limit,
@RequestParam(required = false) String sort, // JSON 數組字符串
@RequestParam(required = false) String filter // JSON 數組字符串
) {
Pageable pageable = PageRequest.of(offset / limit, limit, parseSort(sort));
Specification spec = parseFilter(filter);
Page page = userRepository.findAll(spec, pageable);
return new PageResult<>(page.getTotalElements(), page.getContent());
}
Ext.define('App.store.BigDataStore', {
extend: 'Ext.data.Store',
buffered: true, // 啟用緩衝
pageSize: 1000, // 預加載塊大小
leadingBufferZone: 200, // 可視區上方預加載
trailingBufferZone: 200, // 可視區下方預加載
proxy: { type: 'ajax', url: '/api/bigdata', reader: { type: 'json', rootProperty: 'data', totalProperty: 'total' } },
remoteSort: true,
remoteFilter: true
});
// Grid 無需 PagingToolbar,直接垂直滾動自動加載
totalProperty 對應字段、後端 count 查詢是否同條件過濾。remoteSort:true、sortParam 映射正確、後端解析 sort JSON。store.currentPage 只讀,用 store.loadPage(n) 程式化跳轉。purgePageCount 限制緩存頁數。