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

How to change fancytree SelectMode on a button click event?

我正在使用名为 fancytree (Here is the link)

的树 view/tree 网格插件

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

        // 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(","));
        }
    })

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

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

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

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