如何在初始化时使 jstree 为空?
How to make jstree empty on initialization?
单击按钮后,我将一些密钥发送到控制器并使用 json 获取列表,列表中的数组充当我的 jstree 中的 children,
$("#btnSearch").on("click", function () {
alert("I'm also here");
$.ajax({
type: "POST",
url: "/Claims/JSTree",
success: function (response) {
var clients = [];
var claimKeys = [];
$.each(response.ClientNames, function (i, item) {
clients.push(item);
});
alert(clients);
$.each(response.ClaimNumbers, function (i, item) {
claimKeys.push(item);
});
alert(claimKeys);
$('#FolderTree').jstree({
'core': {
'data': [
{
'text': 'Client',
'state': {
'opened': false,
'selected': false
},
'children': clients
},
{
'text': 'Claim key',
'state': {
'opened': false,
'selected': false
},
'children': claimKeys
},
]
},
"plugins": ["checkbox"]
});
}
});
});
});
第一次一切正常,当我第二次传递一个不同的键时,我得到了列表,它甚至显示了警报中的值,但我的 jstree 仍然保留以前的值..除非我停止并重新启动调试, jstree 不会变空..有没有办法在用 children 填充它之前清空 jstree??
尝试使用此代码,它首先销毁实例:
...
var tmp = $('#FolderTree').jstree(true);
if(tmp) { tmp.destroy(); }
$('#FolderTree').jstree({ ...
还要确保您使用的是最新的 jsTree 版本。
单击按钮后,我将一些密钥发送到控制器并使用 json 获取列表,列表中的数组充当我的 jstree 中的 children,
$("#btnSearch").on("click", function () {
alert("I'm also here");
$.ajax({
type: "POST",
url: "/Claims/JSTree",
success: function (response) {
var clients = [];
var claimKeys = [];
$.each(response.ClientNames, function (i, item) {
clients.push(item);
});
alert(clients);
$.each(response.ClaimNumbers, function (i, item) {
claimKeys.push(item);
});
alert(claimKeys);
$('#FolderTree').jstree({
'core': {
'data': [
{
'text': 'Client',
'state': {
'opened': false,
'selected': false
},
'children': clients
},
{
'text': 'Claim key',
'state': {
'opened': false,
'selected': false
},
'children': claimKeys
},
]
},
"plugins": ["checkbox"]
});
}
});
});
});
第一次一切正常,当我第二次传递一个不同的键时,我得到了列表,它甚至显示了警报中的值,但我的 jstree 仍然保留以前的值..除非我停止并重新启动调试, jstree 不会变空..有没有办法在用 children 填充它之前清空 jstree??
尝试使用此代码,它首先销毁实例:
...
var tmp = $('#FolderTree').jstree(true);
if(tmp) { tmp.destroy(); }
$('#FolderTree').jstree({ ...
还要确保您使用的是最新的 jsTree 版本。