console
var jsonDataTree;
function transData(a, idStr, pidStr, chindrenStr) {
var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
for (; i < len; i++) {
hash[a[i][id]] = a[i];
}
for (; j < len; j++) {
var aVal = a[j], hashVP = hash[aVal[pid]];
if (hashVP) {
!hashVP[children] && (hashVP[children] = []);
hashVP[children].push(aVal);
} else {
r.push(aVal);
}
}
return r;
}
//测试方法
function test() {
//json串
var jsonData = { myobj:[
{id: 0, name: "基础要素", parentLayerId: -1, defaultVisibility: true, subLayerIds: Array(11)},
{id: 1, name: "领海基点", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 2, name: "水闸", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 3, name: "水文观测站", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 4, name: "海岸线", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 5, name: "海域行政界线", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 6, name: "海底光缆", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 7, name: "海岛普查", parentLayerId: 0, defaultVisibility: true, subLayerIds: Array(4)},
{id: 8, name: "有居民海岛", parentLayerId: 7, defaultVisibility: true, subLayerIds: null},
{id: 9, name: "无居民海岛", parentLayerId: 7, defaultVisibility: true, subLayerIds: Array(2)},
{id: 10, name: "冲击沙岛", parentLayerId: 9, defaultVisibility: true, subLayerIds: Array(3)},
{id: 11, name: "冲击沙岛", parentLayerId: 10, defaultVisibility: true, subLayerIds: null},
{id: 12, name: "显示", parentLayerId: 10, defaultVisibility: true, subLayerIds: null},
{id: 13, name: "显示", parentLayerId: 10, defaultVisibility: true, subLayerIds: null},
{id: 14, name: "基岩岛", parentLayerId: 9, defaultVisibility: true, subLayerIds: Array(3)},
{id: 15, name: "基岩岛", parentLayerId: 14, defaultVisibility: true, subLayerIds: null},
{id: 16, name: "显示", parentLayerId: 14, defaultVisibility: true, subLayerIds: null},
{id: 17, name: "显示", parentLayerId: 14, defaultVisibility: true, subLayerIds: null},
{id: 18, name: "低潮高地", parentLayerId: 7, defaultVisibility: true, subLayerIds: Array(3)},
{id: 19, name: "低潮高地", parentLayerId: 18, defaultVisibility: true, subLayerIds: null},
{id: 20, name: "显示", parentLayerId: 18, defaultVisibility: true, subLayerIds: null},
{id: 21, name: "显示", parentLayerId: 18, defaultVisibility: true, subLayerIds: null},
{id: 22, name: "暗礁", parentLayerId: 7, defaultVisibility: true, subLayerIds: null},
{id: 23, name: "12海里领海线", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 24, name: "领海基线", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 25, name: "海域分界线", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 26, name: "省界", parentLayerId: 0, defaultVisibility: true, subLayerIds: null},
{id: 27, name: "海域使用项目", parentLayerId: -1, defaultVisibility: true, subLayerIds: Array(7)},
{id: 28, name: "非透水构筑物", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 29, name: "港池、蓄水等", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 30, name: "海底电缆管道", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 31, name: "建设填海造地", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 32, name: "透水构筑物", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 33, name: "污水达标排放", parentLayerId: 27, defaultVisibility: true, subLayerIds: null},
{id: 34, name: "其他", parentLayerId: 27, defaultVisibility: true, subLayerIds: null}]
};
jsonData = jsonData.myobj;
//console.log(jsonData)
//绑定的字段
jsonDataTree = transData(jsonData, 'id', 'parentLayerId', 'items');
console.log(jsonDataTree);
}
//窗体加载执行的方法
test();
// window.onload=function(){alert(1)} //后执行
//alert(2) //先执行
$("#treeview").kendoTreeView({
checkboxes: {
checkChildren: true
},
dataTextField: "name",
dataSource: jsonDataTree
});
var treeview = $("#treeview").data("kendoTreeView");
treeview.expandTo(1);
<div id="treeview"></div>