Generate a multi-lever array sub-category for a menu

8 followers

This wiki article has not been tagged with a corresponding Yii version yet.
Help us improve the wiki by updating the version information.

Everyone have seen a multilayer menu in several CMS

How to do that using a table ? Lets start!

your model should have id, parent_id and title, where parent_id refers to another record of the same table

Also you should create an extra relation

public function relations() {
        return array(
                         ....
            'Childs' => array(self::HAS_MANY, 'yourModel', 'parent_id'),
        );
  }

Now, in your controller (or component) create this methods

private static $menuTree = array();
 
   public static function getMenuTree() {
        if (empty(self::$menuTree)) {
            $rows = YourModel::model()->findAll('parent_id IS NULL');
            foreach ($rows as $item) {
                self::$menuTree[] = self::getMenuItems($item);
            }
        }
        return self::$menuTree;
    }
 
    private static function getMenuItems($modelRow) {
 
        if (!$modelRow)
            return;
 
        if (isset($modelRow->Childs)) {
            $chump = self::getMenuItems($modelRow->Childs);
            if ($chump != null)
                $res = array('label' => $modelRow->title, 'items' => $chump, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
            else
                $res = array('label' => $modelRow->title, 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
            return $res;
        } else {
            if (is_array($modelRow)) {
                $arr = array();
                foreach ($modelRow as $leaves) {
                    $arr[] = self::getMenuItems($leaves);
                }
                return $arr;
            } else {
                return array('label' => ($modelRow->title), 'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)));
            }
        }
    }

And in your theme call the first method

$this->widget('zii.widgets.CMenu', array(
              'items' =>$this->getMenuTree()
));

Now you have make a multi-layer menu

Total 7 comments

#15117 report it
KonApaz at 2013/10/08 03:34pm
RE: CSS for multi lever menu

Hi blaces

If you want to set css for each level you could write css like that

ul li {
background: #f00;
}
 
ul li ul li {
background: #0f0;
}
 
ul li ul li ul li {
background: #00f;
}
#15113 report it
blaces at 2013/10/08 12:23pm
What kind of CSS do I use for the CMenu?

Hi,

Are there good CSS solution for CMenu to view the multi level menu items? :)

#14707 report it
KonApaz at 2013/09/04 05:40pm
RE: Left Menu Bar

Hi

as the left menu is the same with main menu (there are different css) yes it is possible! just add class zii.widgets.CMenu and manipulate the alignments by css

$this->widget('zii.widgets.CMenu', array(
              'items' =>$this->getMenuTree()
              'htmlOptions'=>array('class'=>'vertical-menu'),
));
#14706 report it
Shahzad Thathal at 2013/09/04 03:21pm
Left Menu Bar

Hi,

I need to show left menu bar with categories and sub categories on home page, Is it possible?

#13861 report it
KonApaz at 2013/07/02 04:38pm
About structure table
CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) DEFAULT NULL,
  `title` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`);
);
#13853 report it
KonApaz at 2013/07/02 04:48am
About permissions

Hi mahdi1986

Thank you for your comment

You could use Yii::app()->user->checkAccess('whattask') foreach

('label' => ($modelRow->title), 
'visible'=> Yii::app()->user->checkAccess('the_task'.$modelRow->id)
'url' => Yii::app()->createUrl('yourcontroller/youraction', array('id' => $modelRow->id)))

but you have to know about the roles permissions, check that http://www.yiiframework.com/doc/guide/1.1/en/topics.auth and http://www.yiiframework.com/wiki/328/simple-rbac/

best regards

#13852 report it
mahdi1986 at 2013/07/02 03:38am
DataBase

Hi, Thanks for this wiki

Can you tell us structure table in database and how can I check user have permission to item Menu with ACL??

for example: at below code I can check only admin user can access see item Users

array('label'=>'Users','items'=>array(
                            array('label'=>'Create','url'=>array('/authItem/create'),
                                'visible'=>Yii::app()->user->name=="admin"
                            ),
                            array('label'=>'Admin','url'=>array('/authItem/admin'),
                                **'visible'=>Yii::app()->user->name=="admin"**
                            )
                        )),

sorry for bad english :D

Leave a comment

Please to leave your comment.

Write new article