Helper- menu and active submenu (codeigniter)

1

Hello! I'm using the helper menu to include class active in the site menus.

Helper Menu:

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed');
    if(!function_exists('active_link'))
    {
        function menu_ativo($controller)
        {
        //Obtem Instância da classe CI
        $CI = get_instance();
        //Obtem classe ativa.
        $class = $CI->router->fetch_class();
        return ($class == $controller) ? 'active' : '';
       }
    }
?>

And I invoke this function as below

<li class="treeview <?php echo menu_ativo('conta'); ?>">
   <a href="#">
   <i class="fa fa-money"></i>
   <span><?php echo 'Financeiro'; ?></span>
   <i class="fa fa-angle-left pull-right"></i>
   </a>
   <ul class="treeview-menu">
      <li class="<?php echo menu_ativo('categoria'); ?>">
         <a href="<?php echo site_url('app/categoria'); ?>"><i class="fa fa-circle-o"></i>Categoria</a>
      </li>
      <li class="<?php echo menu_ativo('conta'); ?>">
         <a href="<?php echo site_url('app/conta'); ?>"><i class="fa fa-circle-o"></i>Conta</a>
      </li>
      <li class="<?php echo menu_ativo('encargo'); ?>">
         <a href="<?php echo site_url('app/encargo'); ?>"><i class="fa fa-circle-o"></i>Encargo</a>
      </li>
      <li class="<?php echo menu_ativo('lancamento'); ?>">
         <a href="<?php echo site_url('app/lancamento'); ?>"><i class="fa fa-circle-o"></i>Lançamento</a>
      </li>
   </ul>
</li>

My problem is basically in <li class="treeview <?php echo " ???? " ;?>">

Since every <li> within <ul> belongs to a different controller, I want to activate any sub-menu and keep the main menu active <li> . But with the above helper not working, although the submenu is inheriting class active , the main menu is closing.

Can you guide me?

    
asked by anonymous 20.06.2017 / 23:09

1 answer

2

I would do it as follows:

<li class="<?php if($this->uri->segment(1)=='categoria') echo "active"; ?>">
     <a href="<?php echo site_url('app/categoria'); ?>"><i class="fa fa-circle-o"></i>Categoria</a>
</li>

In this case URI->SEGMENT at position (1) would represent the URL: app/categoria . Then you need to test, being echo $this->uri->segment(1) to see what returns, then know what position your module is in.

Edit: In case of being submenu, you can do a validation between modules, if($this->uri->segment(1)=='categoria' or $this->uri->segment(1)=='categoriaB' or $this->uri->segment(1)=='categoriaC') echo "active";

Or if you do not want to do this, place on top of each module, in the construct:

$this->session->set_userdata('modulo_ativo', 'Financeiro');

Then you do:

if($this->session->userdata('modulo_ativo')!=NULL && $this->session->userdata('modulo_ativo')=='Financeiro') echo "active"; 

This is when it comes to the main menu, not submenu.

    
20.06.2017 / 23:42