itstructure/yii2-multi-level-menu Widget for rendering multilevel menu for Yii2 Framework

menumultilevel

Yii2 Multilevel menu widget

Introduction

Latest Stable Version Latest Unstable Version License Total Downloads Build Status Scrutinizer Code Quality

This widget is designed to display a multilevel menu, in which there can be nested submenus. Used for Yii2 framework.

The widget uses data from the database, in which there are, in addition to the primary keys, also the parent keys.

Data from the database is taken from an active model, which instance of yii\db\ActiveRecord.

Multi level menu example scheme

Requirements

  • php >= 7.1
  • composer

Installation

Via composer:

composer require itstructure/yii2-multi-level-menu ~3.2.9

Usage

Usage in view template

Base application config must be like in example below:

use Itstructure\MultiLevelMenu\MenuWidget;
echo MenuWidget::widget([
    'menuId' => 'multi-level-menu',
    'data' => array_values($dataProvider->getModels()),
    'itemTemplate' => '@app/views/MultiLevelMenu/main.php'
]);

Example of itemTemplate file:

use yii\helpers\{Url, Html};
/* @var app\models\Page $data */
<span>
    <?php echo Html::a(
        Html::encode($data->title),
        Url::to(['view', 'id' => $data->id])
    ) ?>
</span>

Example when there are some properties for nesting levels:

use Itstructure\MultiLevelMenu\MenuWidget;
echo MenuWidget::widget([
    'menuId' => 'multi-level-menu',
    'data' => array_values($dataProvider->getModels()),
    'itemTemplate' => '@app/views/MultiLevelMenu/main.php'
    'mainContainerOptions' => [
        'class' => 'list-group'
    ],
    'itemContainerOptions' => [
        'levels' => [
            ['class' => 'list-group-item'],
            ['class' => 'list-group-item list-group-item-success'],
            ['class' => 'list-group-item list-group-item-warning'],
        ]
    ],
]);

Example when there are some properties as callable function:

use Itstructure\MultiLevelMenu\MenuWidget;
echo MenuWidget::widget([
    'menuId' => 'multi-level-menu',
    'data' => array_values($dataProvider->getModels()),
    'itemTemplate' => '@app/views/MultiLevelMenu/main.php'
    'mainContainerOptions' => function () {
        return [
            'class' => $level == 0 ? 'nav navbar-nav navbar-right' : 'dropdown-menu'
        ];
    },
    'itemTemplateParams' => function ($level, $item) {
        return [
            'linkOptions' => isset($item['items']) && count($item['items']) > 0 ? [
                'class' => 'dropdown-toggle',
                'data-toggle' => 'dropdown',
                'aria-haspopup' => 'true',
                'aria-expanded' => 'false',
            ] : [],
        ];
    },
    'itemContainerOptions' => function ($level, $item) {
        return $level == 0 ? [
            'class' => isset($item['items']) && count($item['items']) > 0 ? 'nav-item dropdown' : 'nav-item'
        ] : [
            'class' => isset($item['items']) && count($item['items']) > 0 ? 'dropdown-item dropdown' : 'dropdown-item'
        ];
    }
]);

Database table structure example

Table "pages"

| id  | parentId |   title    | ... |
|-----|----------|------------|-----|
|  1  |   NULL   |   item 1   | ... |
|  2  |   NULL   |   item 2   | ... |
|  3  |   NULL   |   item 3   | ... |
|  4  |   NULL   |   item 4   | ... |
|  5  |   NULL   |   item 5   | ... |
|  6  |     2    |  item 2.1  | ... |
|  7  |     2    |  item 2.2  | ... |
|  8  |     7    | item 2.2.1 | ... |
|  9  |     7    | item 2.2.2 | ... |
|  10 |     7    | item 2.2.3 | ... |
| ... |    ...   |     ...    | ... |

License

Copyright © 2018-2023 Andrey Girnik girnikandrey@gmail.com.

Licensed under the MIT license. See LICENSE.txt for details.

Changelog

CHANGE LOG:

3.2.9 January 30, 2023:

  • Upgrade copyright year.

3.2.8 February 23, 2021:

  • Upgrade copyright year.

3.2.7 August 11, 2020:

  • Add new param $parentItem to renderItems() private method.

3.2.6 July 17, 2020:

  • Readme instruction fixes.

3.2.5 June 23, 2020:

  • Modify README syntax.

3.2.4 May 10, 2019:

  • Support callable type for the next attributes:
    • mainContainerTag
    • mainContainerOptions
    • itemContainerTag
    • itemContainerOptions
    • itemTemplate
    • itemTemplateParams

3.2.3 August 9, 2018:

  • Code fixes according with the PSR standards.

3.2.2 June 27, 2018:

  • Modify groupLevels() function for the count of models.

3.2.1 June 27, 2018:

  • Modify levelAttributeValue() function.

3.2.0 June 8, 2018:

  • Added ability to get value of itemContainerTag and mainContainerTag attributes for each nesting level.

3.1.0 May 24, 2018:

  • Add menuId attribute.

3.0.0 May 23, 2018:

  • Remove sub attributes.
  • Add levelAttributeValue() function.
  • Now you can set attribute values for each nesting level.
  • Add addition function checkNewParentId() to check whether a particular record can be used as a parent.

2.0.0 May 22, 2018:

  • Delete an ActiveDataProvider. And instead, it simply uses the data parameter (array of ActiveRecord).

1.1.0 May 20, 2018:

  • Add sub item and sub container options to separate them with the main container and item options.

1.0.0 May 19, 2018:

  • Create widget with the first release.

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

Comments



3.2.9 is the latest of 15 releases



MIT license
Stats
5 github stars & 1 github forks
0 downloads in the last day
3 downloads in the last 30 days
294 total downloads