In jsTree, I want to draw a new tree replacing the previous one and have nodes initially selected in the new tree.
In the code below, I confirmed that state : {opened : true}
made the node initially opened and state : {disabled : true}
made the node initially disabled, so I thought state : {selected : true}
works too, but actually it is not.
Please note that specifying state : {selected : true}
when calling $('#using_json').jstree({...});
does result in the node selected as described in https://www.jstree.com/docs/json/. Then, I wonder why state : {selected : true}
does not work when I replace a existing tree with a new tree (while state : {opened : true}
and state : {disabled : true}
are working).
How can we achieve nodes to be initially selected after replacing tree?
$('#using_json').jstree({ 'core' : {
data : [
{
text : 'Previous root node',
}
]
} });
const newData = [
{
text : 'New root node (Initially opened)',
state : {
opened : true, //'opened' takes effect after refresh
},
children : [
{ text : 'Child 1 (Initially disabled)',
state : {
disabled : true, //'disabled' takes effect after refresh
}
},
{ text : 'Child 2 (Intended to be selected initially but failing)',
state : {
selected : true //'selected' does NOT take effect after refresh
}
}
]
}
]
$('#using_json').jstree(true).settings.core.data = newData;
$('#using_json').jstree(true).refresh(true);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.4/themes/default/style.min.css" />
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.4/jstree.min.js"></script>
<div id="using_json"></div>
</body>
</html>