ztree將後臺傳過來的json物件封裝為子父集關係的json陣列
1、將後臺傳過來的資料封裝
var initDataValue = function (resp) {
var initDataVO = {};initDataVO.value = [];
var regionDataList = resp.data || [];
$.each(regionDataList, function( index, data ) {
if ( data.parentid == 0 ) {
data.value = [];
initDataVO = data;
} else if ( data.parentid == 1 ) {
data.value = [];
initDataVO.value.push(data);
}
});
$.each(initDataVO.value || [], function( index, dataVO ) {
$.each(regionDataList, function( index, regionData ) {
if ( regionData.parentid == dataVO.id ) {
dataVO.value.push(regionData);
}
})
})
return initDataVO;
};
2、呼叫行政區域資料
regionTree.getRegionList().success(function(regionResp){
if ( regionResp && regionResp.code == "200" ) {
var zNodes = regionTree.initTreeData(
initDataValue(regionResp) );
setTimeout(function() {
$.fn.zTree.init($("#treeDemo"), regionTree.setting, zNodes);
}, 100);
}
});
3、行政區域的配置及方法
/**
* VIM行政區域資源樹
* author : jgx
* date : 2015.12.18
*/
define([
'js/pvd/monitor/zTree_v3/js/jquery.ztree.all-3.5.js',
'js/pvd/monitor/zTree_v3/js/jquery.ztree.core-3.5.min.js'
], function(){
var regionTree = {
setting: {
check: {
enable: true
},
data: {
simpleData: {
enable: true,
rootPId: 0
}
},
callback:{
onCheck: function(e, treeId, treeNode){
var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = treeObj.getCheckedNodes(true),
checkedName = [],
checkedAreaId = [];
for(var i = 0;i < nodes.length; i++){
checkedName.push(nodes[i].name);
checkedAreaId.push(nodes[i].areaId);
//alert(nodes[i].areaId + ":::" + nodes[i].name); //獲取選中節點的值
}
}
}
},
/**
* 獲取行政區域結果集
*/
getRegionList: function(){
var retDef = jQuery.Deferred();
return jQuery.ajax({
type: "post",
url: "/videoview/organization/selectOrg?orgId=1",//介面地址
dataType: "json",
data: {},
success: function(regionResp){
if ( regionResp && regionResp.code == "200" ) {
retDef.resolve(regionResp);
}
}
});
return retDef;
},
initTreeData: function(result) {
var zNodes = [];
if (result) {
var fatherID = "1";
var fatherPID = "0";
zNodes.push({id: fatherID, pId: fatherPID, areaId:result.id, name: result.name, open: false});
var arrList = result.value;
for (var i = 0; i < arrList.length; i++) {
var firstIndex = i + 1;
var firstChildID = fatherID + firstIndex;
zNodes.push({id: firstChildID, pId: "1", areaId: arrList[i].id, name: arrList[i].name, open: false});
var arrValueList = arrList[i].value;
for (var x = 0; x < arrValueList.length; x++) {
var secondIndex = x + 1;
var secondChildID = firstChildID + secondIndex;
zNodes.push({id: secondChildID, pId: "1" + firstIndex, areaId: arrValueList[x].id, name: arrValueList[x].name});
}
}
}
return zNodes;
}
};
return regionTree;
});