热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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

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

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

我正在使用jsTree 1.0从2011年7月25日从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

其中demo1指的是a

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

我是根据这个链接做到这一点的,Ivan似乎主张这个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

然而,正在发生的事情是,在第二次调用init时,我最终在firebug中发现了这个错误

instance._get_settings is not a function

instance._get_settings不是函数

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?

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

2 个解决方案

#1


13  

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

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

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

#2


9  

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

我有同样的问题。版本jsTree - 3.0。*。 A已经在下一个方面解决了它:

$(function (){

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

推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 标题: ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 用Vue实现的Demo商品管理效果图及实现代码
    本文介绍了一个使用Vue实现的Demo商品管理的效果图及实现代码。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
  • 1.Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • Iamtryingtocreateanarrayofstructinstanceslikethis:我试图创建一个这样的struct实例数组:letinstallers: ... [详细]
  • 在C#中,使用关键字abstract来定义抽象类和抽象方法。抽象类是一种不能被实例化的类,它只提供部分实现,但可以被其他类继承并创建实例。抽象类可以用于类、方法、属性、索引器和事件。在一个类声明中使用abstract表示该类倾向于作为其他类的基类成员被标识为抽象,或者被包含在一个抽象类中,必须由其派生类实现。本文介绍了C#中抽象类和抽象方法的基础知识,并提供了一个示例代码。 ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
author-avatar
亲爱one
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有