【问题标题】:Display error message if data is null for jstree.core.data如果 jstree.core.data 的数据为空,则显示错误消息
【发布时间】:2019-09-19 13:30:55
【问题描述】:

我有一个来自返回数据调用的 jstree 构建节点。但是有时它会返回 null,我需要通过在 UI 上显示一些错误消息来处理这个问题。我试图对返回的数据添加检查,但它一直显示加载图标,而不是显示我想要的 div 和消息。我的代码:

$('#jstree-test').jstree({
    core: {
        data: function (node, cb) {
            $.getJSON()
                .done(function (data, status, xhr) {
                if(data == null) 
                {                       
                    $('#Error').show();
                }
                else
                {
                    globfunc.redirect(xhr);
                    cb.call(this, data);    
                    }   

                })
                .fail(function (data, status, xhr) {
                    if (xhr.hasOwnProperty("status")) {
                        globfunc.redirect(xhr);
                    } else {
                        globfunc.showModal(xhr);
                    }
                });
        }
    }
})

我需要做些什么来实现这一点吗?

【问题讨论】:

    标签: javascript jstree


    【解决方案1】:

    我建议稍微改变策略。这可能是一种方法,可以尝试。

    首先调用你的ajax函数来获取数据。 那么如果有数据不为null,就初始化jsTree,

    否则按您的意愿处理场景。

    如下所示。以下代码未经测试,仅供参考。有关此方法的详细信息,您可以参考https://everyething.com/Example-of-simple-jsTree-with-dynamic-JSON-data

    <script type="text/javascript">
            $(function () {
                $.getJSON()
                    .done(function (data, status, xhr) {
                    if(data == null) 
                    {                       
                        $('#Error').show();
                    }
                    else
                    {
                        globfunc.redirect(xhr);
                        createJSTree(data);    
                        }   
    
                    })
                    .fail(function (data, status, xhr) {
                        if (xhr.hasOwnProperty("status")) {
                            globfunc.redirect(xhr);
                        } else {
                            globfunc.showModal(xhr);
                        }
                    });
            });
    
            function createJSTree(data) {            
                $('##jstree-test').jstree({
                    'core': {
                        'data': data
                    }
                });
            }
        </script>
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多