<table id="dg" class="easyui-treegrid" title="數據字典列表"
data-options="idField:'id',treeField: 'text',method: 'post',striped:true,toolbar:'#tb',pagination:false,rownumbers:true,singleSelect:true,url:'@Url.Action("LoadData","SystemSet")',onBeforeExpand:onbeforeExpand">
<thead>
<tr>
<th data-options="field:'text',width:320">名稱</th>
<th data-options="field:'id',width:220">編號</th>
<th data-options="field:'state',width:220">狀態</th>
<th data-options="field:'value',width:220">值</th>
<th data-options="field:'操作',width:120,align:'center'" formatter="formatCount">操作</th>
</tr>
</thead>
</table>
</div>
1 function onbeforeExpand(row) { 2 //動態設置展開查詢的url 3 var url = '/SystemSet/LoadData?id=' + row.id; 4 $("#dg").treegrid("options").url = url; 5 return true; 6 }
第二種:得到充分數據,按層次惰性加載節點,首先只加載頂層節點
<table id="dg" class="easyui-treegrid" title="數據字典列表" data-options="idField:'id',treeField: 'text',method: 'post',striped:true,toolbar:'#tb',pagination:false,rownumbers:true,singleSelect:true,url:'@Url.Action("LoadData","SystemSet")',loadFilter: myLoadFilter"> <thead> <tr> <th data-options="field:'text',width:320">名稱</th> <th data-options="field:'id',width:220">編號</th> <th data-options="field:'state',width:220">狀態</th> <th data-options="field:'value',width:220">值</th> <th data-options="field:'操作',width:120,align:'center'" formatter="formatCount">操作</th> </tr> </thead> </table> </div>
為了放置加載子節點,我們需要為每個節點重命名 'children' 屬性。 正如下面的代碼所示,'children' 屬性重命名為 'children1'。 當展開一個節點時,我們調用 'append' 方法來加載它的子節點數據。
'loadFilter' 代碼
1 function myLoadFilter(data, parentId) { 2 function setData() { 3 var todo = []; 4 for (var i = 0; i < data.length; i++) { 5 todo.push(data[i]); 6 } 7 while (todo.length) { 8 var node = todo.shift(); 9 if (node.children) { 10 node.state = 'closed'; 11 node.children1 = node.children; 12 node.children = undefined; 13 todo = todo.concat(node.children1); 14 } 15 } 16 } 17 setData(data); 18 var tg = $(this); 19 var opts = tg.treegrid('options'); 20 opts.onBeforeExpand = function (row) { 21 if (row.children1) { 22 tg.treegrid('append', { 23 parent: row[opts.idField], 24 data: row.children1 25 }); 26 row.children1 = undefined; 27 tg.treegrid('expand', row[opts.idField]); 28 } 29 return row.children1 == undefined; 30 }; 31 return data; 32 }
