Unless the data is very large, load it all in the browser to start with, and toggle the visibility on and off.
Use https://github.com/lazychaser/laravel-nestedset for the back-end
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have table with hierarchy and nested rows. So at first I only display the highest hierarchy in 1 row. Then on click of it, it should open next level, and so on. (Need to be able to fold it back too)
But the initial load of the data of each next hierarchy is done with AJAX so the rows are dynamically added.
This is a basic structure of what I mean:
Main Row
|______SubData1
| |______SubSubData1
| |______SubSubData2
|______SubData2
| |______SubSubData3
| |______SubSubData4
| |______SubSubData5
| |______SubSubData6
|______SubData3
| |______SubSubData7
|______SubData4
|______SubSubData8
|______SubSubData9
|______SubSubData10
It's a nested tree but the actual table will not be nested (When you open up the next hierarchy it should just open up rows below the clicked parent)
How should I go about doing that if the number of nested elements is not known?
I was thinking about assigning data attributes to the rows based on the table row id in the DB, then add listeners using jQuery for click events. Then whenever a user clicks on this row (maybe put a little "expand" arrow), it will send an AJAX request to the next level in the hierarchy, then append the data as new table rows using after the specific clicked parent.
Example: I click on the Main Row - it sends AJAX and gets all SubDatas from 1 to 4, and appends them below it.
Then if I click of SubData2, it will send another AJAX and append the four SubSubDatas between the row of SubData2 and SubData3
Is it OK? Is there anything I need to think about before starting? Maybe because the table loads dynamically then something about the CSS? Because I know there are some troubles with tables and CSS when the size is not known
Thanks!
Please or to participate in this conversation.