如何使用树帮助器在cakephp 2中创建动态菜单?

时间:2016-05-20 05:35:25

标签: php css cakephp tree view-helpers

我正在使用cakephp 2 Tree Helper和bootstrap js / css来创建动态菜单。

我想要以下输出:

<ul class="nav navbar-nav">
          <li class="active"><a href="#"> Home </a></li>

      <li class="dropdown">
          <a data-toggle="dropdown" class="dropdown-toggle" href="#">About Us <span class="caret"></span></a>
          <ul class="dropdown-menu md-menu">
           <li><a href="#">Styles </a></li>
          </ul>
        </li>

      </ul>

我在beforeFilter中尝试过这段代码:  /var/www/html/cakephp/app/Controller/AppController.php

  <?php
            $this->loadModel('Menue');
            $this->Menue->recursive = -1;
           $menues =  $this->Menue->find('threaded');
           $this->set(compact('menues'));

//无功/网络/ HTML / cakephp的/应用/视图/布局/ frontend.ctp

 <?php
    echo $this->Tree->generate(
            $menues,
            array(
                    'element' => 'node',
                    'model' => 'Menue',
                    'class' => 'nav navbar-nav',
            )
    );

//无功/网络/ HTML / cakephp的/应用/视图/元件/ node.ctp

<?php
    $this->Tree->addItemAttribute('id', 'menu_'.$data['Menue']['id']);


    if ($depth == 0)
    {
      $this->Tree->addItemAttribute('class', 'depth_zero');
    }

    $dataToggle = '';
    if ($hasChildren)
    {

        $dataToggle = 'data-toggle="dropdown"';

        $this->Tree->addTypeAttribute('class', 'dropdown-menu md-menu', null, 'previous');


     $this->Tree->addItemAttribute('class', 'dropdown');
    }



    $is_current = (isset($menue_node_id) && $menue_node_id == $data['Menue']['id'])? ' class="Current"': '';

    echo '<a '.$dataToggle.'  href="'.$data['Menue']['slug_url'].'" class="dropdown-toggle" rel="'.$data['Menue']['id'].'"'.$is_current.'>'.$data['Menue']['name'].'</a>'; 
    ?>

目前我收到了这个输出:

<ul class="nav navbar-nav">
    <li class="depth_zero" id="menu_10"><a rel="10" class="dropdown-toggle" href="home">HOme</a>



    </li>
    <li class="depth_zero dropdown" id="menu_11"><a rel="11" class="dropdown-toggle" href="about_us" data-toggle="dropdown">About Us</a>




    <ul>
        <li id="menu_12"><a rel="12" class="dropdown-toggle" href="style">style</a>



        </li>
    </ul>
    </li>
</ul>

菜单的SQL结构:

CREATE TABLE IF NOT EXISTS `menues` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `is_for_header_or_footer` tinyint(1) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `lft` int(11) NOT NULL,
  `rght` int(11) NOT NULL,
  `name` varchar(100) NOT NULL DEFAULT '',
  `slug_url` varchar(200) NOT NULL,
  `redirect_url` varchar(200) NOT NULL,
  `redirect_target` varchar(200) NOT NULL,
  `created` datetime NOT NULL,
  `created_by` int(11) unsigned NOT NULL,
  `modified` datetime NOT NULL,
  `modified_by` int(11) unsigned NOT NULL,
  `status` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;

我也查了这个网址,但没有成功: http://www.dereuromark.de/2013/02/17/cakephp-and-tree-structures/

基本上我想将类添加到内部ul标记。

由于

0 个答案:

没有答案