D
D
drboboev2017-05-18 09:44:57
PHP
drboboev, 2017-05-18 09:44:57

How to display a Nested Set tree with a grouping of nodes?

Good afternoon experts.
After a long search for a solution to my problem, I began to delve a little into the Nested Set. I liked the model, it is very convenient to sample data. BUT ran into a problem. Although, perhaps, this is not a problem, but for some reason I cannot figure out how to implement it.
I have a certain tree in the database, for example, the site menu. One type request: I get the whole tree. But I want to build the site menu so that the categories and subcategories can be collapsed. Those. The website menu looks like this:
SELECT * FROM my_tree ORDER BY left_key

<ul>
  <li>Категория 1
    <ul>
      <li>Пункт меню 1.1</li>
      <li>Пункт меню 1.2</li>
    </ul>
  </li>
  <li>Категория 2
    <ul>
      <li>Пункт меню 2.1</li>
      <li>Пункт меню 2.2</li>
    </ul>
  </li>
</ul>

Please tell me how, without loading the database with unnecessary requests and without piling up the code with a bunch of cycles, I can build a menu of this kind.
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arman, 2017-05-18
@drboboev

Tree traversal without recursion , but simply by recursion bypass the records and collect the tree.

K
Kain_Wesker, 2019-08-28
@Kain_Wesker

I don’t know how to display a menu without cycles, but in order for categories and subcategories to be collapsed, I can advise interesting modules:
https://github.com/dbushell/Nestable - it is quite simple and provides collapse.
https://github.com/RamonSmit/Nestable2 - completion of the upper module with improved ability to set events.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question