I am trying to dynamically add a new node in my jsTree container. It is not working at all. I don't know what i am missing.
jsfiddle example
$("#tree").jstree({
core: {
"animation": 0
},
"html_data": {},
"themes": {
"icons": false
},
"search": {
"case_insensitive": true,
"show_only_matches": true
},
"plugins": ["themes", "html_data", "search"]
});
$("#tree").jstree("create_node", $("node_1"), "after", { "data": "Hello World" });
html:
<div id="tree">
<ul>
<li id="node1"><a>Hello</a></li>
</ul>
</div>
set 'check_callback' : true, otherwise all operations like create, rename are prevented. like so:
this.treeDiv.jstree(
{
'core' : {
'check_callback': true,
'data' : {
'url' : function (node) {
return 'ajax_roots.json';
},
'data' : function (node) {}
}
},
"search" : {
"fuzzy" : false,
"show_only_matches": true,
"close_opened_onclear" : true
},
"plugins" : ["search", "sort", "json_data"]
});
Apart from setting core.check_callback to true, it's worth to mention that creating a simple doesn't require so many arguments. You could simply achieve it by doing:
$('#tree').jstree(true).select_node('nodeid');
var tr = $('#tree').jstree(true),
selected = tr.get_selected();
selected = tr.create_node(selected, {"type":"file(or any other type you need)"});
Hope this could help someone. I had the problem and tried to follow the API but couldn't work;instead i inspected the codes from demos and found that.
I don't know what is causing this, but adding a setTimeout when creating the node fixes the problem
setTimeout(function() {
$("#tree").jstree("create_node", $("node_1"), "after", { "data": "Hello World" });
}, 1000);
Demo simple what you need http://jsfiddle.net/gwxTa/2/ or http://jsfiddle.net/gwxTa/ or Dynamic - (click add button) http://jsfiddle.net/VBSJ8/ or http://jsfiddle.net/ak4Ed/
Please see my old post: jsTree not working
code from dynamic add button functionality:
$(function() {
$("#tree").jstree({
"json_data": {
"data": [
{
"data": "Hello",
"attr": {
"id": "root.id"
},
"children": [{
"data": "Hello World",
"attr": {
"id": "node_1"
},
"children": []}
]},
]
},
"plugins": ["themes", "json_data", "crrm"]
});
});
$("#tree").jstree("create", $("#node_1"), "inside", {
"data": "child2"
}, function() {
alert("added");
}, true);
Hope you are including the scripts:
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript' src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<script type='text/javascript' src="http://static.jstree.com/v.1.0pre/jquery.hotkeys.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With