Array
(
[11] => Array
(
[id] => 11
[name] => one
[description] => first
[child] => Array
(
[33] => Array
(
[id] => 33
[name] => three
[description] => third
)
)
)
[22] => Array
(
[id] => 22
[name] => two
[description] => second
[child] => Array
(
[11] => Array
(
[id] => 11
[name] => one
[description] => first
[child] => Array
(
[33] => Array
(
[id] => 33
[name] => three
[description] => third
)
)
)
[33] => Array
(
[id] => 33
[name] => three
[description] => third
)
)
)
[33] => Array
(
[id] => 33
[name] => three
[description] => third
)
)
private function getChildsForView($child){
$this->list .= 'Collapse::widget([\'items\' => [';
foreach($child as $k => $v){
$this->list .= '[
\'label\' => \'' . $v['name'] . ' - ' . $v['description'] . '\'';
$this->list .= ',\'content\' => ';
if(isset($v['child'])) {
$this->list .= self::getChildsForView($v['child']);
} else $this->list .= '\'\',]';
$this->list .= ',],';
}
$this->list .= '])';
}
Collapse::widget(
[
'items' => [
[
'label' => 'one - first',
'content' => Collapse::widget(
[
'items' => [
[
'label' => 'three - third',
'content' => '',
],
],
]),
],
[
'label' => 'two - second',
'content' => Collapse::widget(
[
'items' => [
[
'label' => 'one - first',
'content' => Collapse::widget(
[
'items' => [
[
'label' => 'three - third',
'content' => '',
],
],
]),
],
[
'label' => 'three - third',
'content' => '',
],
],
]),
],
[
'label' => 'three - third',
'content' => '',
],
],
]);
<?= \path\to\widget\MyWidget::widget( [ 'list' => $treeList ] ); ?>
class MyWidget extends \yii\bootstrap\Widget
{
public $list = [];
public function someMethod () {}
public function run () {
return $this->renderFile( '...' );
}
}
<?php
use path\to\widget\MyWidget;
use yii\web\View;
/**
* @var View $this
* @var MyWidget $widget
*/
$widget = $this->context;
?>
<!-- Тут мы используя циклы и методы виджета формируем отображение -->
<?php foreach( $widget->... as $value ) { ?>
<!-- Делаем что нужно и как нужно -->
<?php } ?>
private function getChildsForView($child){
$this->c++;
foreach($child as $k => $v){
$this->list .= '<div class="panel panel-default">';
$this->list .= '<div class="panel-heading tick" role="tab" id="heading' . $this->c . '" style="cursor: pointer" data-toggle="collapse" data-parent="#accordion" href="#collapse' . $this->c . '" aria-controls="collapse' . $this->c . '">';
$this->list .= '<h4 class="panel-title">';
$this->list .= '<img align="middle" id="greentick" src="../images/greenTick.png" class="greenTick" style="display: none">';
$this->list .= $v['name'];
if(isset($v['child']) && count($v['child']) > 0) $this->list .= ' ' . Html::badge(count($v['child']));
$this->list .= '</h4>';
$this->list .= '</div>';
if(isset($v['child']) && count($v['child']) > 0){
$this->list .= '<div id="collapse' . $this->c . '" class="panel-collapse collapse out" role="tabpanel" aria-labelledby="heading' . $this->c . '">';
$this->list .= '<div>';
$this->list .= '<ul>';
foreach($v['child'] as $kk => $vv){
$this->list .= '<li>';
$this->list .= self::getChildsForView($v['child']);
$this->list .= '</li>';
}
$this->list .= '</ul>';
$this->list .= '</div>';
$this->list .= '</div>';
}
$this->list .= '</div>';
}
}