0

How to create a nested lists in Yii2? Not a menu lists. I followed Yii2 docs.

So, in params.php in config directory I have an array. The code is like this:

<?php
return [
'version' => 'framework: ' . Yii::getVersion() . ' version: ' . '0.0.0',
'description' => [
    'app' => [
        'PT. ABC' => [
            'feature' => [
                'Request IT-06',
                'PEB'
            ],
            'bug' => [
                'No Bug'
            ],
            'changelog' => [
                'Initialize Program'
            ]
        ],
        'PT. XYZ' => [
            'feature' => [
                'Request IT-06',
                'PEB'
            ],
            'bug' => [
                'No Bug'
            ],
            'changelog' => [
                'Initialize Program'
            ]
        ],
    ],
],
];

I want to make a nested lists. I just know to display the index of array like this:

-PT. ABC
-PT. XYZ

This is my code:

<div class="col-lg-8">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Welcome to <?= Yii::$app->params['version'] ?></h3>
        </div>
        <div class="panel-body">
            <?= Html::ul(Yii::$app->params['description']['app'], [
                'item' => function ($item, $index) {
                    return Html::tag(
                        'li',
                        $index,
                        ['class' => 'post']
                    );
                }
            ]) ?>
        </div>
    </div>
</div>

I need Like this,for example,

-PT. ABC
  * feature
     - feature 1
  * bug
  * changelog
-PT. XYZ
  * feature
  * bug
  * changelog
1
  • you can do using for loop Commented Apr 10, 2018 at 5:11

1 Answer 1

1

You need a recurrency for this. Create your own Html helper with method for that:

public static function nestedUl($items) {
    return Html::ul($items, [
        'encode' => false,
        'item' => function ($item, $index) {
            if (is_array($item)) {
                $content = Html::encode($index) . Html::nestedUl($item);
            } else {
                $content = Html::encode($item);
            }

            return Html::tag(
                'li',
                $content,
                ['class' => 'post']
            );
        }
    ]);
}

It will generate nested list for each array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.