Answer the question
In order to leave comments, you need to log in
Trouble creating a hierarchical tree with jQuery?
I'm trying to make a "hierarchical tree" for one of my projects (essentially an analogue of jQuery Treeview , jsTree and similar plugins), which also uses the drag & drop functionality. I use jQuery UI to enable drag & drop . Everything seems to work, but stalled at one point.
Lyrical digression: yes, I know that this is essentially writing a bicycle, and that you can not worry and take one of the common plugins, but it is important for me to learn how to do different tricks on jQuery with drag & drop, because it will be needed in many more places in my project.
So what we have. There is an unordered list <ul></ul> whose elements are categories. Categories can have subcategories, and those, in turn, have their own subcategories, and so on. If a category has subcategories, then a plus icon is displayed to the left of it, clicking on which will make a request to the server and in response we will receive the necessary list of subcategories, which will be automatically inserted as a sublist, i.e. like this:
<ul><br>
<li class="categories close" id="cat1" rel="1">category A</li><br>
<li class="categories open" id="cat2" rel="2">category B</li><br>
<ul id="p2"><br>
<li class="categories" id="cat4" rel="4">subcategory D</li><br>
<li class="categories" id="cat5" rel="5">subcategory E</li><br>
<li class="categories" id="cat6" rel="6">subcategory F</li><br>
</ul><br>
<li class="categories close" id="cat3" rel="3">category C</li><br>
</ul><br>
...<br>
over:function(event,ui) {<br>
_cury = ui.position.top;<br>
_curx = ui.position.left;<br>
...<br>
}<br>
...<br>
...<br>
ui.position.top = _cury;<br>
ui.position.left = _curx;<br>
...<br>
...<br>
out:function(event,ui) {<br>
ui.position.top = _cury;<br>
ui.position.left = _curx;<br>
...<br>
}<br>
...<br>
Answer the question
In order to leave comments, you need to log in
I advise you to see how it is done in the latest WordPress'e there is a similar drag and drop in the menu. It is sometimes useful to look at someone else's code.
Digression: yes, I know that according to the rules of HTML syntax, the sublist <ul id="p2"></ul> should be inside the parent <li></li> element, but in this case, along with the category "category B" will be to drag around the cursor and the entire sublist of subcategories, which we don’t want in any way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question