Creating a root node dynamically (on init)

849 views
Skip to first unread message

Roman Exotic

unread,
Oct 2, 2015, 12:06:25 PM10/2/15
to jsTree
Hello, Ivan. On my system, your plugin use as a to-do list. It sounds a bit strange, but I don't know the other way. 

Never mind, Imagine the task, for example, go shopping as one of the nodes of the tree. In your plugin node can not be displayed without a root node. But a root node will look distracting. So I would like to know how I can hide a root element and add it at the initialization, because there is a problem with the select data. Or, if possible, create a root node only memory. I tried this:

tree(opt).on('init.jstree', function () {
tree.jstree('create_node', '#', { 'attr' : { 'id' : 1 } , 'text' : 'Root'}, 'first');
});

This has no effect. I currently use this to fill in a tree, because each user has his root. It is not comfortable: 

'data': {
'url': 'task/node',
'cache': true,
'data': function (node) {
return { 'id': node.id };
}
}

What do you advise, Ivan?

Roman Exotic

unread,
Oct 5, 2015, 5:15:51 AM10/5/15
to jsTree
Okay, I solved this problem. But why do I have the wrong IDs? j1_1, j1_2 etc. instead of 1, 2? Wait a minute, if the user waits a moment before pressing ESC, then all will be right here:
tree.jstree(true).edit(node, false, function (node) {
$(document).on('keyup', function (evt) {
//setTimeout(function () {
if (evt.keyCode == 27) {
tree.jstree(true).delete_node(node.id);
}
//}, 600);
});
});
Here is my config:
'core':        {
    'data': {
'url': 'task/node',
'cache': true,
'data': function (node) {
return { 'id': node.id };
}
    },
'check_callback': true,
'multiple': false,
'themes': {
name: 'proton',
url: 'vendor/plugins/jstree/themes/proton/style.css',
responsive: true
}
},
'checkbox': {
'three_state': false,
'cascade': 'down'
},
'search': {
"show_only_matches": true
},

Nothing extra.
пятница, 2 октября 2015 г., 19:06:25 UTC+3 пользователь Roman Exotic написал:

Ivan Bozhanov

unread,
Oct 5, 2015, 5:47:08 AM10/5/15
to jsTree
Do not use "attr" - there is no such property on the node. Simply use: "id" : "value":
tree.jstree('create_node', '#', { 'id' : 1, 'text' : 'Root'}, 'first');

Best regards,
Ivan

Roman Exotic

unread,
Oct 6, 2015, 3:56:34 PM10/6/15
to jsTree
Thanks! But now I have another problem. When I add a node, jstree adds a few more nodes with the name of 'New node'. This code helps not to add them to the database:
$.get('task/create', {
'id': data.node.parent,
'position': data.position,
'text': data.node.text
}).done(function (d) {
data.instance.set_id(data.node, d.id);
if (data.node.text == 'New node')
tree.jstree(true).delete_node(data.node.id); // Sometimes 'New node' hide when the page is reloaded.
})

I use a form to add own nodes:
var obj = tree.jstree(true).get_node($("a:contains('Root')"), true);
if ($("a:contains('Root')").parent('li').children('.jstree-children').length == 0) { // It is necessary to update the tree again
tree.jstree(true).create_node(obj, { text: textField.val() }, "last", null, true);
tree.jstree(true).settings.core.data = {
url: 'task/node',
        data: function (node) {
return { id: node.id };
}
    };
tree.jstree(true).refresh();
rootHasChildren.length = 1;
} else {
    tree.jstree(true).create_node(obj, { text: textField.val() }, "last", null, true);
}

If I create an error before creating the node, the nodes 'New node' are not creating. What can be done in this case? Thanks in advance.

пятница, 2 октября 2015 г., 19:06:25 UTC+3 пользователь Roman Exotic написал:
Hello, Ivan. On my system, your plugin use as a to-do list. It sounds a bit strange, but I don't know the other way. 

Ivan Bozhanov

unread,
Oct 7, 2015, 8:40:34 AM10/7/15
to jsTree
Sorry but I can not reproduce this - can you provide a demo. I can not understand how a few more nodes are shown - maybe you have an event handler bound a few times?

Best regards,
Ivan
Reply all
Reply to author
Forward
0 new messages