Yii2中的嵌套列表

问题描述 投票:0回答:1

如何在Yii2中创建嵌套列表?不是菜单列表。我跟着Yii2 docs

所以,在config目录中的params.php中我有一个数组。代码是这样的:

<?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'
            ]
        ],
    ],
],
];

我想制作一个嵌套列表。我只知道显示数组的索引,如下所示:

-PT. ABC
-PT. XYZ

这是我的代码:

<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>

我需要像这样,例如,

-PT. ABC
  * feature
     - feature 1
  * bug
  * changelog
-PT. XYZ
  * feature
  * bug
  * changelog
php yii2
1个回答
0
投票

你需要一个可重复的货币。使用以下方法创建您自己的Html助手:

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']
            );
        }
    ]);
}

它将为每个数组生成嵌套列表。

© www.soinside.com 2019 - 2024. All rights reserved.