【问题标题】:Refresh JStree data刷新 JStree 数据
【发布时间】:2020-02-11 11:04:01
【问题描述】:

我正在使用JSTree,为此,我加载了数据,但在某些情况下,我需要使用另一个即将到达的数据库过滤器重新加载数据。但是如何将数据添加到JSTRee,它始终保留旧数据。

我在complete中尝试如下:

complete: function() { a $('#mytree').jstree(true).refresh();

但没有变化。

function loadTreeContent(bindTo)
{
    $.ajax({
        type: 'get',
        url: bindTo,
        dataType: 'json',
        success: function(data) {
            $('#mytree').jstree({
                'core' : {
                    'data' : data
                },
                'checkbox' : {
                    "keep_selected_style" : false
                },
                "plugins" : [ 'checkbox', 'contextmenu', 'state'],
                'checkbox': {
                    three_state : true,
                    whole_node : false,
                    tie_selection : false
                },
                'contextmenu': {
                    'items': function ($node) {
                        var tree = $('#mytree').jstree(true);
                        if($node.a_attr.type === 'test')
                            return getTestContextMenu($node, tree);
                    }
                }
            });
        },
        complete: function() {
            // This function will be triggered always, 
            // when ajax request is completed, even it fails/returns other status code
            console.log("complete");
        },
        error: function() {
            // This will be triggered when ajax request fail.
            console.log("Error");
        }
    });
};

【问题讨论】:

  • 一种方法是销毁并重新初始化 jstree 或者您可以使用 redraw(true)

标签: javascript jstree


【解决方案1】:

尝试redraw 而不是refresh

complete: function() { a $('#mytree').jstree(true).redraw(true);

【讨论】:

    【解决方案2】:

    试试:

    $(".jstree").jstree(true).refresh(true, true);
    

    如果你在 jstree 中使用 state (checked and selected) 插件,那么刷新方法就会变得复杂。 refresh方法有两个参数

    refresh(skip_loading, forget_state)
    

    默认情况下:

    1. skip_loading: {Boolean} skip_loading 跳过显示加载指示器的选项
    2. forget_state: {Mixed} forget_state 如果设置为true 状态将不会被重新应用,如果设置为函数(接收当前状态作为参数)的结果。因此,默认情况下,如果您不提供任何值,它将在刷新之前应用现有状态。

    所以,我的情况是我需要清理我以前的状态,然后我需要调用

    $(".jstree").jstree(true).refresh(true, true);
    

    https://github.com/vakata/jstree/issues/2112#issuecomment-421551095 和源代码:https://github.com/vakata/jstree/blob/master/src/jstree.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多