【问题标题】:JS Tree - select_node not workingJSTree - select_node 不工作
【发布时间】:2012-10-10 10:56:55
【问题描述】:

我对 JSTree 和 Ajax 有一个奇怪的问题。

我通过 Ajax/PHP 请求生成我的树,该请求使用...生成 HTML(带有 UL、LI、A 标签)

$.ajax({
    url: 'ajaxTreeView.php?method=edit&partid='+partId,
    cache: false,
    success: function(tree)
    {
        $('#treeViewer').html(tree);
    }});

并使用...激活代码上的 JStree。

options = 
{
    "core": { animation: 120 },
    "themes": { theme: 'corral', dots: true },
    "types": 
    { 
        "types": 
        { 
            "parent": { "icon": { "image": "images/custom/Legend/node_select.png" } },
            "child": { "icon": { "image": "images/custom/Legend/node_select_child.png" } },
            "current": { "icon": { "image": "images/custom/Legend/node.png" } }
        }
    },
    "plugins": [ "html_data", "types", "themes", "ui", "contextmenu", "cookies" ],
    "ui": { "select_limit" : 1 },
    "cookies": { "save_selected" : false }
};

$("#tree").jstree(options);

我似乎无法轻松选择节点。我尝试了initial_select,但这似乎不起作用,也不理想,因为我经常想以编程方式选择节点。

我尝试使用...

$('#tree').jstree("select_node", '#ref565', true);

如果我通过超链接调用该函数,则此方法有效,但如果我在初始化 JStree 后调用它,则此方法无效。

我从添加警报中看到(所有这些都发生在 Ajax Success 例程中)...

$('#treeViewer').html(tree);
$("#tree").jstree(options);
alert('test');
$('#tree').jstree("select_node", '#ref565', true);

...在警报开始之前树尚未呈现。

我添加了一个 setTimeOut...

$('#treeViewer').html(tree);
$("#tree").jstree(options);
setTimeout(function() {selectNode(565);},1250);
$('#tree').jstree("select_node", '#ref565', true);

...这行得通。

我显然是愚蠢的。我使用了错误的语法吗?为什么我必须设置延迟才能选择节点?

请帮忙。

【问题讨论】:

    标签: jquery ajax jstree


    【解决方案1】:

    如果您想在树加载后最初选择某些节点,您应该使用ui 插件的initially_select 选项。你说你试过了,但我没有看到你发布的示例代码中使用了它。您确定您提供的 ID 正确吗?

    要以编程方式选择节点,您需要先等待要选择的节点出现在 DOM 中。而不是使用超时回调,我猜绑定到 loaded.jstree 事件更正确,应该在树完成加载并且所有树元素都在 DOM 之后调用该事件,并进行编程选择在那里。

    显示用法的示例代码:

    $(function () {
    
        $("#tree")
            .jstree({ 
                // List of active plugins
                "plugins" : [ 
                    "ui"
                ],
    
                // UI & core - the nodes to initially select and open will be overwritten by the cookie plugin
                // the UI plugin - it handles selecting/deselecting/hovering nodes
                "ui" : {
                    // this makes the node with ID node_4 selected onload
                    "initially_select" : [ "#ref565" ]
                },
                // the core plugin - not many options here
                "core" : { 
                    // just open those two nodes up
                    // as this is an AJAX enabled tree, both will be downloaded from the server
                    "initially_open" : [ "#ref565" ] 
                }
            })
            .bind("loaded.jstree", function (e, data) {
                      $('#tree').jstree("select_node", '#ref565', true); 
            }) 
    });
    

    【讨论】:

    • 这正是我想要的,但我在谷歌搜索后看到很多以 .bind 开头的代码。我很笨,这是否意味着我应该写... $('#tree').jstree.bind("loaded.jstree", function(event, data) { $('#tree').jstree ("select_node", '#ref565', true); });如果是这样,那对我不起作用。
    • 我更新了我的示例,展示了如何绑定事件。如果由于编码错误而无法正常工作,请使用调试器解决。
    • 非常感谢。我发现现在理解这些事件要容易得多,因为我意识到它们可以在树的实例化时预先绑定。很好的例子,干杯:)
    • 很好的例子,博金。如果树是延迟加载的,initially_open 是否通过 Ajax 加载子节点?
    • 完美答案,对我真的很有用:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多