使用新的 json 数据重新创建整个 jstree 实例

发布于 2024-11-26 04:51:45 字数 2211 浏览 2 评论 0原文

我希望用新的 json 数据替换 jstree 树的全部内容。

我正在使用 2011 年 7 月 25 日从 github 下载的 jsTree 1.0

假设我有这个功能...

function init_my_tree(my_json_data)
{
  $("#demo1").jstree({
    themes: {
      "theme": "classic",
      "dots": false,
      "icons": true,
      "url": "//js.myliburl.com/jstree/themes/classic/style.css"
    },
    json : {
      data : my_json_data
    },
    plugins : [ "core","ui","themes", "json" ]
  });
}

其中 demo1 指的是

<div id="demo1"></div> 

我想做的是用我从服务器加载的新数据完全替换该树。然而,出于这个问题的目的,让我们假装我只想这样做......

$(document).ready(function() {
  var text_data = '[{"title":"All","data":{"jstree":{"opened":true}},"children":[{"title":"Item 1","data":{"jstree":{}},"children":false,"li_attr":{"id":"1","class":"jstree-leaf","href":"#"},"a_attr":{"href":"#"}},{"title":"Item B","data":{"jstree":{}},"children":false,"li_attr":{"id":"2","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}],"li_attr":{"id":"0","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}]';
  var my_json_data = $.parseJSON(text_data); 
  init_my_tree(my_json_data);  // initialize the tree view

  text_data = '[{"title":"Something Else","data":{"jstree":{"opened":true}},"children":[{"title":"Item A","data":{"jstree":{}},"children":false,"li_attr":{"id":"1","class":"jstree-leaf","href":"#"},"a_attr":{"href":"#"}},{"title":"Item 2","data":{"jstree":{}},"children":false,"li_attr":{"id":"2","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}],"li_attr":{"id":"0","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}]';
  my_json_data = $.parseJSON(text_data); 
  init_my_tree(my_json_data);  // re-initialize the tree view to load with new data

});

我是根据这个链接来做这个的,伊万似乎提倡这个 http://groups.google.com/group/jstree/browse_thread/thread/b40a1f0ab0f9a66b? fwc=2

但是,发生的情况是,在第二次调用 init 时,我最终得到了这个萤火虫中的错误

instance._get_settings 不是函数

我尝试调用 destroy

$("#demo1").jstree("destroy");

的函数,但这并没有解决我的问题。

如何用新的 json 数据替换整个树?

I wish to replace the entire contents of a jstree tree with new json data.

I'm using jsTree 1.0 downloaded July 25, 2011 from github

Say I have this function...

function init_my_tree(my_json_data)
{
  $("#demo1").jstree({
    themes: {
      "theme": "classic",
      "dots": false,
      "icons": true,
      "url": "//js.myliburl.com/jstree/themes/classic/style.css"
    },
    json : {
      data : my_json_data
    },
    plugins : [ "core","ui","themes", "json" ]
  });
}

where demo1 refers to a

<div id="demo1"></div> 

What I'm trying to do is to completely replace the tree with new data that I load from my server. For purposes of this question, however, let's pretend I just want to do this...

$(document).ready(function() {
  var text_data = '[{"title":"All","data":{"jstree":{"opened":true}},"children":[{"title":"Item 1","data":{"jstree":{}},"children":false,"li_attr":{"id":"1","class":"jstree-leaf","href":"#"},"a_attr":{"href":"#"}},{"title":"Item B","data":{"jstree":{}},"children":false,"li_attr":{"id":"2","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}],"li_attr":{"id":"0","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}]';
  var my_json_data = $.parseJSON(text_data); 
  init_my_tree(my_json_data);  // initialize the tree view

  text_data = '[{"title":"Something Else","data":{"jstree":{"opened":true}},"children":[{"title":"Item A","data":{"jstree":{}},"children":false,"li_attr":{"id":"1","class":"jstree-leaf","href":"#"},"a_attr":{"href":"#"}},{"title":"Item 2","data":{"jstree":{}},"children":false,"li_attr":{"id":"2","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}],"li_attr":{"id":"0","class":"jstree-last","href":"#"},"a_attr":{"href":"#"}}]';
  my_json_data = $.parseJSON(text_data); 
  init_my_tree(my_json_data);  // re-initialize the tree view to load with new data

});

I'm doing this based on this link, where Ivan seems to advocate this
http://groups.google.com/group/jstree/browse_thread/thread/b40a1f0ab0f9a66b?fwc=2

However, what's happening is that, on the 2nd call to init, I end up geting this error in firebug

instance._get_settings is not a function

I tried calling destroy

$("#demo1").jstree("destroy");

but that didn't fix my problem.

How can I replace the entire tree with new json data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-12-03 04:51:45

我是这样解决这个问题的:
- 将树的所有绑定和初始化包装在一个函数中
- 从我的 document.ready 函数调用此函数(这处理树的初始设置)
- 在我的 ajax 调用的成功回调中,我销毁树,然后再次调用该函数

以下是代码:

function loadTargetTree() {
    $("#target-book-tree").bind("select_node.jstree", function (e, data) {
      data.rslt.obj; // the LI node that was clicked
      selectedTargetID = data.rslt.obj.attr("id");
      if(data.rslt.obj.hasClass("book") || data.rslt.obj.hasClass("section")) {
        targetChapterIsSelected = false;
        toggleCopyTools()
      } else {
        targetChapterIsSelected = true;
        toggleCopyTools();
        target_parent_id = selectedTargetID;
      }
    });

    $("#target-book-tree")
        .jstree({
            core : {
                "initially_open" : ["book_"+getParameterByName('target_book_id')],
                "animation": 100
                },
            "plugins":["themes","html_data","ui"],
            "themes" : {
                "theme" : "classic",
                "dots" : true,
                "icons" : false
                },
            "ui" : {
                "select_limit" : 1,
                "selected_parent_close" : "deselect",
                },
        });
}


$(document).ready(function() {
    loadTargetTree();
});



AND MY AJAX CALL:

    var sendData = 'new_name='+$('input#new_name').val()+'&target_book_id='+target_book_id + '&source_lib_id='+source_lib_id + '&target_parent_id='+target_parent_id;
    $.ajax({
          dataType: "json",
       type: "POST",
       url: "/ajax/CopySelectedSection",
       data: sendData,
        error: function(data) {
            alert("Oops! An error occured. Please try again later.")
        },
        success: function(data) {
                if (data['status'] == "ok") {
                    $("div#target-book-tree").html(data['book_outline']);
                    $("#target-book-tree").jstree('destroy');
                    loadTargetTree();
                } else {
                    alert("Sorry, ...");
                }
        }
     })

Here's how I solved this problem:
- wrap all the bindings and initialization of the tree in a function
- call this function from my document.ready function (this handles initial setup of the tree)
- in the success callback of my ajax call, I destroy the tree and then call the function again

Here's the code:

function loadTargetTree() {
    $("#target-book-tree").bind("select_node.jstree", function (e, data) {
      data.rslt.obj; // the LI node that was clicked
      selectedTargetID = data.rslt.obj.attr("id");
      if(data.rslt.obj.hasClass("book") || data.rslt.obj.hasClass("section")) {
        targetChapterIsSelected = false;
        toggleCopyTools()
      } else {
        targetChapterIsSelected = true;
        toggleCopyTools();
        target_parent_id = selectedTargetID;
      }
    });

    $("#target-book-tree")
        .jstree({
            core : {
                "initially_open" : ["book_"+getParameterByName('target_book_id')],
                "animation": 100
                },
            "plugins":["themes","html_data","ui"],
            "themes" : {
                "theme" : "classic",
                "dots" : true,
                "icons" : false
                },
            "ui" : {
                "select_limit" : 1,
                "selected_parent_close" : "deselect",
                },
        });
}


$(document).ready(function() {
    loadTargetTree();
});



AND MY AJAX CALL:

    var sendData = 'new_name='+$('input#new_name').val()+'&target_book_id='+target_book_id + '&source_lib_id='+source_lib_id + '&target_parent_id='+target_parent_id;
    $.ajax({
          dataType: "json",
       type: "POST",
       url: "/ajax/CopySelectedSection",
       data: sendData,
        error: function(data) {
            alert("Oops! An error occured. Please try again later.")
        },
        success: function(data) {
                if (data['status'] == "ok") {
                    $("div#target-book-tree").html(data['book_outline']);
                    $("#target-book-tree").jstree('destroy');
                    loadTargetTree();
                } else {
                    alert("Sorry, ...");
                }
        }
     })
神爱温柔 2024-12-03 04:51:45

我也有同样的问题。版本 jsTree - 3.0.*。 A已通过以下方式解决了这个问题:

$(function (){

            var data = JSON.parse('<?php echo $treedata;?>');
            initTree(data);
            var data1 = JSON.parse('<?php echo $terminsData;?>');
            var flag = 0;
            $("#butree").on("click", function () {
                if (flag) {
                    $("#termtree").jstree("destroy");
                    initTree(data);
                    flag = 0;
                } else {
                    $("#termtree").jstree("destroy");
                    initTree(data1);
                    flag = 1;
                }
            });
        });

I had the same problem. Version jsTree - 3.0.*. A have solved it in the next way:

$(function (){

            var data = JSON.parse('<?php echo $treedata;?>');
            initTree(data);
            var data1 = JSON.parse('<?php echo $terminsData;?>');
            var flag = 0;
            $("#butree").on("click", function () {
                if (flag) {
                    $("#termtree").jstree("destroy");
                    initTree(data);
                    flag = 0;
                } else {
                    $("#termtree").jstree("destroy");
                    initTree(data1);
                    flag = 1;
                }
            });
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文