用新的json数据重新创建整个jstree实例 [英] Recreate entire jstree instance with new json data

查看:74
本文介绍了用新的json数据重新创建整个jstree实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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" ]
  });
}

其中demo1指的是

<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

});

我正在基于此链接进行此操作,Ivan似乎在倡导此操作 http://groups.google.com/group/jstree/browse_thread/thread/b40a1f0ab0f9a66b? fwc = 2

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

但是,发生的是,在对init的第二次调用中,我最终在萤火虫中得到了此错误

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

instance._get_settings不是函数

instance._get_settings is not a function

我试图打电话给毁灭

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

但这并不能解决我的问题.

but that didn't fix my problem.

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

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

推荐答案

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

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

代码如下:

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

这篇关于用新的json数据重新创建整个jstree实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆