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行政区域资源树
* 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;
});