如何在按钮单击事件上更改fancytree SelectMode?

时间:2017-09-28 13:53:35

标签: javascript jquery treeview fancytree

我正在使用一个名为fancytree(Here is the link)的树视图/树网格插件

我最初(当页面加载时)将selectMode设置为2,允许用户选择多个选项。

        // Init 
    $("#Organizations .OrgTree").each(function () {
        var $this = $(this);
        var organizationUnitId = $this.data("orgid");
        //var treeData = JSON.parse($("#treeData_" + organizationUnitId).val());          
        $this.fancytree({
            extensions: ["persist"],
            selectMode: 2,
            persist: {
                expandLazy: true
                store: "auto" 'session': sessionStore
            },
            icons: false,
            checkbox: true,
            toggleEffect: null, //disable animations
            source: window["treeData_" + organizationUnitId], // treeData               
            lazyLoad: function (event, data) {
                var node = data.node;
                data.result = {
                    // Some data
                };
            }
        });
    });

我想要做的是,当我点击一个按钮时,我想更改selectMode并将其设置为1或3。 我试图阅读文档并找到this as well 但我不会工作。这是一些代码:

        //Bind click to search button
    $("#changeSelectModeBtn").click(function () {

        debugger;
        $("#Organizations .OrgTree").fancytree("getTree").visit(function (node) {
            node.setSelected(false);
        });

        $("#Organizations .OrgTree").fancytree({ selectMode: 3 });

        var selectedKeys = [];

        $("#Organizations .OrgTree").each(function () {

            var $this = $(this);
            var orgTree = $(this).fancytree("getTree");
            var selectedParentNodes = orgTree.getSelectedNodes();
            for (node in selectedParentNodes) {
                selectedKeys.push(selectedParentNodes[node].key)
            }

            // Save orgtree in hidden fields
            var organizationUnitId = $this.data("orgid");
            var treeData = orgTree.toDict(true);
            window["treeData_" + organizationUnitId] = treeData.children; //$("#treeData_" + organizationUnitId).val(JSON.stringify(treeData.children))

        });

        if (selectedKeys) {
            $("#HiddenOrganizationUnitIds").val(selectedKeys.join(","));
        }
    })

有人可以帮帮我吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

可以使用jQuery小部件模式动态设置所有选项。 例如,您可以使用

$("#tree").fancytree("option", "selectMode", 3);

另见https://github.com/mar10/fancytree/wiki#configure-options