Widget for rendering multilevel menu for Yii2 Framework
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.scrutinizer.yml
LICENSE
README.md
changelog.md
composer.json

README.md

Yii2 Multilevel menu widget

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

2 Dependencies

  • php >= 7.1
  • composer
  • Yii2 = 2.*

3 Installation

Via composer:

composer require "itstructure/yii2-multi-level-menu": "^3.2.3"

or in section require of composer.json file set the following:

"require": {
    "itstructure/yii2-multi-level-menu": "^3.2.3"
}

and command composer install, if you install yii2 project extensions first,

or command composer update, if all yii2 project extensions are already installed.

4 Usage

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

4.2 Database table structure example

Table "pages"

| id  | parentId | title | ... |
|-----|----------|-------|-----|
|  1  |   NULL   | page1 | ... |
|  2  |   NULL   | page2 | ... |
|  3  |     1    | page3 | ... |
|  4  |     1    | page4 | ... |
|  5  |     4    | page5 | ... |
|  6  |     4    | page6 | ... |
|  7  |     3    | page7 | ... |
|  8  |     3    | page8 | ... |
|  9  |   NULL   | page9 | ... |
|  10 |   NULL   | page10| ... |
| ... |    ...   |  ...  | ... |

License

Copyright © 2018 Andrey Girnik girnikandrey@gmail.com.

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