zTree(六)非同步傳參和非同步重新整理父節點(不固定值,字串型別)
阿新 • • 發佈:2019-02-18
因為涉及到非同步載入節點重新整理問題,所以好好研究了一下zTree
非同步傳參
autoParam:非同步載入時需要自動提交父節點屬性的引數。[setting.async.enable = true 時生效]
otherParam:Ajax 請求提交的靜態引數鍵值對。[setting.async.enable = true 時生效]
看官方文件不難曉得autoParam
是重新整理節點的時候才會提交這個引數,而其中的型別
Array(String) 格式說明
可以為空[ ],如果有 key,則必須存在 value。 例如:[key, value]
JSON 格式說明
直接用 JSON 格式製作鍵值對,例如:{ key1:value1, key2:value2 }
otherParam
是不管重新整理節點還是非同步載入都會攜帶這個引數,而且官方說是靜態引數鍵值對,這裡就涉及到專案的不同了。
因為我專案在獲取組織結構的只能能獲取本地組織結構(parentCode=1
),全域性組織結構暫時不允許獲取(parentCode=0
或者不攜帶這個引數),所以我需要設定otherParam
,又因為我在重新整理節點的時候的引數也是parentCode
,所以就會和autoParam
引數衝突,所以我就只設置了otherParam
,但是由於官方說這個引數是靜態鍵值對,所以我需要將他改為動態值,又因為我提交引數需要是字串格式,所以我的contentType: "application/json"
var param = {
parentCode: '1'
}
async: {
enable: true,
url: ip + "mgmt/getOrg.do",
contentType: "application/json",
autoParam: [],
otherParam: function () {
return param
},
dataFilter: ajaxDataFilter
},
這樣每次提交引數的時候度會動態獲取最新的param
值。
附上父節點和當前節點重新整理程式碼
function refreshNode() {
/*根據 treeId 獲取 zTree 物件*/
var zTree = $.fn.zTree.getZTreeObj("treeOrg"),
type = "refresh",
silent = false,
/*獲取 zTree 當前被選中的節點資料集合*/
nodes = zTree.getSelectedNodes();
console.log(nodes)
/*強行非同步載入父節點的子節點。[setting.async.enable = true 時有效]*/
param = {
parentCode: nodes.orgCode
}
zTree.reAsyncChildNodes(nodes[0], type, silent);
// zTree.reAsyncChildNodes(null, "refresh");
}
function refreshParentNode() {
var zTree = $.fn.zTree.getZTreeObj("treeOrg"),
type = "refresh",
silent = false,
nodes = zTree.getSelectedNodes();
/*根據 zTree 的唯一標識 tId 快速獲取節點 JSON 資料物件*/
var parentNode = zTree.getNodeByTId(nodes[0].parentTId);
// console.log(parentNode)
/*選中指定節點*/
zTree.selectNode(parentNode);
console.log(parentNode)
param = {
parentCode: parentNode.orgCode
}
zTree.reAsyncChildNodes(parentNode, type, silent);
}