In the database there is a data structure Nested sets and a project using yii2 / It is necessary to display this structure, as a nested Collapse
.
Here is the structure sorted by RGT:
The result should be the following:
-*spoiler* --*spoiler* --*spoiler* ----*spoiler* -*spoiler* --*spoiler* ----*spoiler* -*spoiler* -*spoiler* -*spoiler* -*spoiler* -*spoiler* -*spoiler* -*spoiler* --*spoiler* -*spoiler*
Adding spoiler is as follows. This is a yii2 widget that returns the layout of spoilers, with styles already applied and id
on which will be js minimized:
private static function createCollapse($label, $content) { return Collapse::widget( [ 'items' => [ [ //В таблице поле LABEL 'label' => $label, //В качестве контента ID записи 'content' => $content ] ] ] ); }
The layout looks like this:
<div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"><a class="collapse-toggle collapsed" href="#w7-collapse7" data-toggle="collapse" data-parent="#w7" aria-expanded="false">Main Spoiler</a></h4> </div> <div id="w7-collapse7" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;"> <div class="panel-body"> <div id="w6" class="panel-group collapse in" aria-expanded="true" style=""> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"><a class="collapse-toggle collapsed" href="#w6-collapse1" data-toggle="collapse" data-parent="#w6" aria-expanded="false">Sub spoiler</a></h4> </div> <div id="w6-collapse1" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;"> <div class="panel-body"> content </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"><a class="collapse-toggle collapsed" href="#w6-collapse2" data-toggle="collapse" data-parent="#w6" aria-expanded="false">Sub Spoiler</a></h4> </div> <div id="w6-collapse2" class="panel-collapse collapse" aria-expanded="false" style="height: 0px;"> <div class="panel-body"> content </div> </div> </div> </div> </div> </div> </div>
As I understand it, just going through the root for all children will not work, because every top spoiler widget should already be aware of its content, which can in turn consist of an unlimited number of same spoiler widgets, and so on to infinity.
One solution is to get the table sorted by RGT in one request and check the current LVL for 3 cases - more \ less \ equal - an example of implementation with <ul><li>
. But I can’t adapt this example to the use of the widget :(
The second option, as suggested by @fedornabilkin, but nested sets
do not store the parent id
, and again, there is a difficulty with wrapping it in Collapse::widget
I will be glad to any help!