A
A
Alexey Voropaev2016-06-04 18:36:16
Yii
Alexey Voropaev, 2016-06-04 18:36:16

How to add unique id to submenu of menu widget in yii2?

Hello, I'm making a menu using yii\widgets\Menu . I'm using bootstrap's collapse. In collapse, you need to specify an id for the submenu that will appear / disappear. I can't figure out how to make a unique id for all submenus. I found only on the Internet

'submenuTemplate' => "\n<ul id='sidebar1'>\n{items}\n</ul>\n"
This does not suit me, because 1 id is set for all submenus. Help find a solution. I'll post my code if you need it
<?php echo Menu::widget([
                        'items' => [
                            [
                                'label' => 'Пользователи',
                                'options' => [
                                    'data-toggle' => 'collapse',
                                    'data-target' => '#sidebar1',
                                    'class' => 'dropdown'
                                ],
                                'items' => [
                                    [
                                        'label' => 'Основное',
                                        'url' => ['/admin/user'],
                                        'active' => Yii::$app->controller->id == 'user',
                                    ],
                                
                                ]
                            ],
                            [
                                'label' => 'Тестовый пункт',
                                'options' => [
                                    'data-toggle' => 'collapse',
                                    'data-target' => '#sidebar2',
                                    'class' => 'dropdown'
                                ],
                                'items' => [
                                    [
                                        'label' => 'asdasd',
                                        'url' => ['/admin/main'],
                                        'active' => Yii::$app->controller->id == 'test',
                                    ],                                
                                ],
                            ],
                        ],
                        'activateParents'=>true,
                        'labelTemplate'	=>'<div>{label}</div>',
                        'activeCssClass' => 'active',  
                        'submenuTemplate' => "\n<ul <i>id='sidebar1'</i> class='nav collapse'>\n{items}\n</ul>\n",
                        'options' => [
                            'class' => 'nav nav-sidebar'
                        ]
                        
                    ]); ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
latteo, 2016-06-05
@boomrap

https://github.com/yiisoft/yii2-framework/blob/2.0...
By setting submenuTemplate after items you change the default template for submenu.
It is enough to set them inside the items of the first level to get the desired effect.

[
      'label' => 'Тестовый пункт',
      'options' => [
        'data-toggle' => 'collapse',
        'data-target' => '#sidebar2',
        'class' => 'dropdown'
      ],
      'items' => [
        [
          'label' => 'asdasd',
          'url' => ['/admin/main'],
          'active' => Yii::$app->controller->id == 'test',
        ],                
      ],
      'submenuTemplate' => "\n<ul id='sidebar2' class='nav collapse'>\n{items}\n</ul>\n",
    ],

M
Maxim Timofeev, 2016-06-04
@webinar

I think I'll have to write my own widget class, inherit from yii\widgets\Menu and rewrite the menu item render method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question