I'm working on a project which requires a hierarchical navigation menu to be built. jstree looks good for this.
The tree will be saved to a database - I am planning on using CakePHP's Tree Behaviour (the project has to work in Cake 2.x rather than 3.x due to the existing codebase).
One of the things I need to do is have the ability to add "Tags" to my tree from an external data source.
The way I have things configured is as follows:
The data to populate my jstree is coming from a database table (called navigations due to Cake's naming conventions). It uses the table structure given on the Tree Behaviour link above.
I'm loading this data into a jstree with the ajax method:
$.ajax({
type : "GET",
url : "/ajax_get_tree",
dataType : "json",
success : function(json) {
createJSTrees(json);
},
error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
function createJSTrees(jsonData) {
$("#tree").jstree({
'core': {
"check_callback" : true,
'data' : jsonData
},
"plugins" : ["dnd"]
}).on('loaded.jstree', function() {
$("#tree").jstree('open_all');
});
}
What I want to do is drag and drop "Tags" (by which I mean list items) from a separate div, #tagList into elements on the tree. The tag data is formatted as follows:
<div id="tagList">
<li data-tag="1">United Kingdom</li>
<li data-tag="2">France</li>
<li data-tag="3">Germany</li>
</div>
I know it's possible to use things like jqueryui's draggable behaviour to move them from one div to another (from #tagList into #tree).
However, I don't know how I can "drop" the tag such that jstree lists it under the appropriate node.
For example I've tried this - which is just using jqueryui and has nothing to do with jstree other than referencing the div it's running on:
$('#tagList li').draggable({
cursor: 'move',
helper: 'clone',
connectToSortable: "#tree",
});
My question really is whether what I'm trying to do is even possible? I've spent a long time looking into this and feel I'm getting nowhere with it.
Are there any other things out there that can do this type of task? I've had a look but was unable to find anything. Essentially the requirements are the ability to create a tree (name, edit, delete, drag/drop) but then to also bring Tags (<li> elements from the #tagList) into it, and save it.