使用 jQuery EasyUI TreeGrid 綁定巢狀 JSON 資料時,明明資料包含多層子節點,介面卻僅顯示根節點一行。經排查,原為 JSON 字段 children 拼寫為 childern,導致 TreeGrid 無法識別子節點集合。
// HTML 定義
<table id="test" class="easyui-treegrid"
url="data/treegrid_data.json"
idField="id" treeField="name">
<thead>...</thead>
</table>
// JSON 回應(節選)
[{
"id": 1,
"kpiName": "系統模版",
"childern": [ // ❌ 拼寫錯誤:childern
{ "id": 2, "kpiName": "無線接通率" },
{ "id": 3, "kpiName": "RRC建立成功率" }
// ... 更多子節點
]
}]
EasyUI TreeGrid 依據 idField 與 treeField 解析樹狀結構,**子節點集合字段名稱必須為 children**(預設),或在初始化時指定 childrenField: "自訂字段名"。若字段名錯誤,TreeGrid 視為無子節點,僅渲染根層。
childern 改為 children。
// 正確 JSON
[{
"id": 1,
"kpiName": "系統模版",
"children": [ // ✅ 正確拼寫
{ "id": 2, "kpiName": "無線接通率" },
{ "id": 3, "kpiName": "RRC建立成功率" }
]
}]
$('#test').treegrid({
idField: 'id',
treeField: 'kpiName',
childrenField: 'childern' // 指定錯誤字段名
});
| 用途 | 預設字段名 | 可自訂屬性 |
|---|---|---|
| 節點唯一標識 | id | idField |
| 顯示文字 | text | treeField |
| 子節點集合 | children | childrenField |
| 節點狀態 | state | stateField |
| 父節點ID | _parentId | parentField |
treegrid_data.json 實際回應內容。console.log(JSON.stringify(data, null, 2)) 確認字段名。children。一個字母之差,導致整棵樹無法展開。養成對照官方文檔字段名的習慣,可避免此類低級錯誤浪費大量時間。