3

Can anyone help me in the implementation of nested set behavior https://github.com/creocoder/yii2-nested-sets with Yii2 Menu Widget? Basically I want the hierarchical sidebar navigation menu like this http://www.surtex-instruments.co.uk/surgical/general-surgical-instruments

Thanks.

flash_flip
  • 41
  • 5

1 Answers1

0

In your Model class define this functions:

public static function getTree($selected, $search)
{
    $categories = Categories::find()->orderBy('lft')->asArray()->all();
    $tree = [];
    $index = 0;
    foreach ($categories as $category) {
        if ($category['parent_category_id'] === NULL) {
            $tree[$index]['text'] = $category['category_' . Yii::$app->language];
            if ($search) {
                $tree[$index]['href'] = Url::to(['products', 'category' => $category['category_id'], 'search' => $search, 'string' => $category['category_' . Yii::$app->language]]);
            } else {
                $tree[$index]['href'] = Url::to(['products', 'category' => $category['category_id'], 'string' => $category['category_' . Yii::$app->language] ]);
            }
            $tree[$index]['id'] = $category['category_id'];
            if ($selected) {
                if ($selected['category_id'] == $category['category_id']) {
                    $tree[$index]['state']['selected'] = true;
                }
                if ($selected['lft'] >= $category['lft'] && $selected['rgt'] <= $category['rgt']) {
                    $tree[$index]['state']['expanded'] = true;
                }
            }
            if ($category['lft'] + 1 != $category['rgt']) {
                Categories::getNodes($tree[$index], $categories, $selected, $search);
            }
            $index++;
        }
    }

    return $tree;

}

private static function getNodes(&$tree, $categories, $selected, $search)
{
    $index = 0;
    foreach ($categories as $category) {
        if ($tree['id'] == $category['parent_category_id']) {
            $tree['nodes'][$index]['text'] = $category['category_' . Yii::$app->language];
            if ($search) {
                $tree['nodes'][$index]['href'] = Url::to(['products', 'category' => $category['category_id'], 'search' => $search, 'string' => $category['category_' . Yii::$app->language]]);
            } else {
                $tree['nodes'][$index]['href'] = Url::to(['products', 'category' => $category['category_id'], 'string' => $category['category_' . Yii::$app->language]]);
            }
            $tree['nodes'][$index]['id'] = $category['category_id'];
            if ($selected) {
                if ($selected['category_id'] == $category['category_id']) {
                    $tree['nodes'][$index]['state']['selected'] = true;
                }
                if ($selected['lft'] >= $category['lft'] && $selected['rgt'] <= $category['rgt']) {
                    $tree['nodes'][$index]['state']['expanded'] = true;
                }
            }
            if ($category['lft'] + 1 != $category['rgt']) {
                Categories::getNodes($tree['nodes'][$index], $categories, $selected, $search);
            }
            $index++;
        }
    }
}

and use this extension execut/yii2-widget-bootstraptreeview

In the controller file get the menu like this:

public function actionProducts($category = false)
{
    ...
    $data = Categories::getTree($category,'');

In your view file

<?php
     ...
     use execut\widget\TreeView;
     ...

     $onSelect = new JsExpression(<<<JS
     function (undefined, item) {
         window.location.href = item.href;
     }
     JS
     );
?>

<?= TreeView::widget([
'data' => $data,
'template' => TreeView::TEMPLATE_SIMPLE,
'clientOptions' => [
    'onNodeSelected' => $onSelect,
    'onhoverColor' => "#fff",
    'enableLinks' => true,
    'borderColor' => '#fff',
    'collapseIcon' => 'fa fa-angle-down',
    'expandIcon' => 'fa fa-angle-right',
    'levels' => 1,
    'selectedBackColor' => '#fff',
    'selectedColor' => '#2eaadc',
],
]); ?>
tsanchev
  • 407
  • 5
  • 13