简体   繁体   English

剔除可观察数组的值已更新时,如何刷新jstree?

[英]How to refresh jstree when value of knockout observable arrays has been updated?

HTML code: HTML代码:

<div id="jstree_1">
<ul data-bind="foreach: KoObservableArray">
    <li data-bind="attr: {type: type}">
        <a href="#" data-bind="text: text"></a>
        <ul data-bind="foreach: children">
            <li data-jstree='{"icon":"glyphicon glyphicon-leaf"}' data-bind="attr: {id: id, type: type}">
                <a href="#" data-bind="text: text"></a>
            </li>
        </ul>
    </li>
</ul>

Then I will build the jstree with below command: 然后,我将使用以下命令构建jstree:

$('#jstree_1').jstree({
'plugins': ["wholerow", "checkbox"]});

Everything works fine for first initialization. 第一次初始化一切正常。 After that, we update knockout observable array data (KoObservableArray), and we need to refresh the jstree to reflect the new data. 之后,我们更新敲除的可观察数组数据(KoObservableArray),并且需要刷新jstree来反映新数据。

I tried: 我试过了:

$('#jstree_1').jstree(true).refresh();
$('#jstree_1').jstree("refresh");

But none of them works for me. 但是他们都不适合我。 Please help and advices :( 请帮助和建议:(

Added code example to demo my problem. 添加了代码示例以演示我的问题。 http://jsfiddle.net/mang/pyh9m7de/7/ http://jsfiddle.net/mang/pyh9m7de/7/

After click on changeMenu, I expect the Jstree will be refresh to represent new data. 单击changeMenu后,我希望Jstree将刷新以表示新数据。

Use redraw(true) 使用redraw(true)

refresh only goes back to initial state. 刷新仅返回到初始状态。

OKay I managed to solve this. 好吧,我设法解决了这个问题。

You have 2 alternatives: 您有2种选择:

1) Reset the data of your jstree 1)重置您的jstree的数据

$('#jstree_1').jstree(true).settings.core.data = self.KoObservableArray();
$('#jstree_1').jstree(true).refresh();

This method working in Chrome and Firefox, but not in my IE :( 这种方法适用于Chrome和Firefox,但不适用于我的IE :(

2) ugly workarounds - working for me in all browser. 2)丑陋的解决方法-在所有浏览器中为我工作。

remove the jstree from HTML and re-add to the DOM 从HTML删除jstree并重新添加到DOM

    var elem = document.getElementById('jstree_1');
    elem.parentNode.removeChild(elem);

    var newElement = $("<div id='jstree_1'><ul data-bind='foreach: KoObservableArray'><li data-bind='attr: {type: type}'><a href='#' data-bind='text: text'></a><ul data-bind='foreach: children'><li data-jstree='{'icon':'glyphicon glyphicon-leaf'}' data-bind='attr: {id: id, type: type}'><a href='#' data-bind='text: text'></a></li></ul></li></ul></div>").appendTo("#jstree_parent");
    ko.applyBindings(viewmodel, document.getElementById("jstree_1"));

    $('#jstree_1').jstree({
        'plugins': ["wholerow", "checkbox"]
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM