combotree:前端實現多層架構(多選項)
阿新 • • 發佈:2019-02-18
資料型別
[{"id":"1","name":"根節點","dm_sub":"1","pid":null,"checked":false}]
前端呼叫語句
initTree('unitSel', rootPath + "/zzjg/getSessionZzjgTreeDetail.do", "");
方法
/** **初始化combotree start*** */ function initTree(combotreeId, url, rootId) { $("#" + combotreeId).combotree({ lines : true, panelWidth : 200, panelHeight : 300, url : url, // 1、過濾資料顯示 valueField : 'id', textField : 'text', editable : true, cascadeCheck:false, multiple:true, collapse:false, loadFilter : function(data) { data = getDepartmentTree(data[0].id, data); return data; }, onLoadSuccess:function(node1,data){ var ds=$('#unitSel').combotree('tree').tree('find', data[0].id); dept = $("#dept").val(); if(dept ==null || dept ==''){ $('#unitSel').combotree('setValue',data[0].id); }else{ $('#unitSel').combotree('setValue',dept); } $('#unitSel').combotree('tree').tree('select', ds.target); } }); }
function getDepartmentTree(rootId, data) { var departmentTree = null; for ( var i = 0; i < data.length; i++) { if (data[i].id == rootId) { departmentTree = "\"id\":" + data[i].id + ",\"text\":\"" + data[i].name + "\","; // 呼叫findChild方法,開始遍歷整個樹,尋找當前節點的子節點。 var child = findChild(data[i].id, data); if (child != null) { departmentTree += child; } departmentTree = "[{" + departmentTree + "}]"; } } return JSON.parse(departmentTree); }
function findChild(id, queryDepart) { var flag = false; var departmentChild = ''; for ( var i = 0; i < queryDepart.length; i++) { var anotherChild = ''; if (queryDepart[i].pid== id)// 尋找到子節點 { anotherChild = "\"id\":" + queryDepart[i].id + ",\"text\":\"" + queryDepart[i].name + "\","; //三級架構 start: var child = findChild(queryDepart[i].id, queryDepart); if (child != null) { anotherChild = anotherChild + child; } //三級架構 end: if (anotherChild[anotherChild.length - 1] == ',') { anotherChild = anotherChild.substring(0, anotherChild.length - 1) } anotherChild = "{" + anotherChild + "}"; departmentChild += anotherChild + ","; } else { flag = false; } } if (departmentChild != null) { departmentChild = departmentChild.substring(0, departmentChild.length - 1); departmentChild = "\"children\":[" + departmentChild + "]"; } return departmentChild; }