zTree(六)异步传参和异步刷新父节点(不固定值,字符串类型)

因为涉及到异步加载节点刷新问题,所以好好研究了一下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);
}

猜你喜欢

转载自blog.csdn.net/jx950915/article/details/81236029