Store 的 reader 配置中 rootProperty (Ext JS 5+ 取代 root) 與後端 JSON 實際數組字段名不一致。
{
"total": 122,
"offset": 0,
"rows": [
{ "id": "1", "name": "張三", "age": 28 },
{ "id": "2", "name": "李四", "age": 32 }
]
}
Ext.define('MyStore', {
extend: 'Ext.data.Store',
model: 'MyModel',
proxy: {
type: 'ajax',
url: '/api/users',
reader: {
type: 'json',
rootProperty: 'rows', // 必須與 JSON 中數組字段名一致
totalProperty: 'total' // 分頁總數字段
}
},
autoLoad: true
});
| 後端框架 | 常用數組字段 | total 字段 |
|---|---|---|
| Java (Spring MVC) | rows, data, list, content | total, totalElements |
| .NET WebAPI | value, data | @odata.count, total |
| Node.js (Express) | data, results | total, count |
| PHP (Laravel) | data | total |
Ext.getStore('MyStore').getProxy().getReader().rootProperty 確認當前配置。store.getProxy().getReader().setRootProperty('data')。reader: {
type: 'json',
rootProperty: function(response) {
// 支持 rows / data / list 多種字段
return response.rows || response.data || response.list || [];
},
totalProperty: function(response) {
return response.total || response.totalElements || response.count || 0;
}
}
Ext JS 5+ 使用
rootProperty(非root),必須與後端 JSON 數組字段名**完全一致**,否則 Store 無法解析數據導致 Grid 空白。