jquery – JS Tree – select_node不工作

前端之家收集整理的这篇文章主要介绍了jquery – JS Tree – select_node不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对JSTree和Ajax有一个奇怪的问题.

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

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

并使用…激活代码上的JStree

  1. options =
  2. {
  3. "core": { animation: 120 },"themes": { theme: 'corral',dots: true },"types":
  4. {
  5. "types":
  6. {
  7. "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" } }
  8. }
  9. },"plugins": [ "html_data","types","themes","ui","contextmenu","cookies" ],"ui": { "select_limit" : 1 },"cookies": { "save_selected" : false }
  10. };
  11.  
  12. $("#tree").jstree(options);

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

我试过用…

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

如果我通过超链接调用函数但是如果我在初始化JStree之后调用它,则无效.

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

  1. $('#treeViewer').html(tree);
  2. $("#tree").jstree(options);
  3. alert('test');
  4. $('#tree').jstree("select_node",true);

…在警报开始之前树没有渲染.

添加了一个setTimeOut …

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

……这很有效.

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

请帮忙.

解决方法

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

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

显示用法的示例代码

  1. $(function () {
  2.  
  3. $("#tree")
  4. .jstree({
  5. // List of active plugins
  6. "plugins" : [
  7. "ui"
  8. ],// UI & core - the nodes to initially select and open will be overwritten by the cookie plugin
  9. // the UI plugin - it handles selecting/deselecting/hovering nodes
  10. "ui" : {
  11. // this makes the node with ID node_4 selected onload
  12. "initially_select" : [ "#ref565" ]
  13. },// the core plugin - not many options here
  14. "core" : {
  15. // just open those two nodes up
  16. // as this is an AJAX enabled tree,both will be downloaded from the server
  17. "initially_open" : [ "#ref565" ]
  18. }
  19. })
  20. .bind("loaded.jstree",function (e,data) {
  21. $('#tree').jstree("select_node",true);
  22. })
  23. });

猜你在找的jQuery相关文章