Helper-menu and active submenu (codeigniter)

Asked

Viewed 294 times

1

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

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 each <li> inside <ul> belongs to a different controller, I want to activate any sub-menu and keep the main-menu active <li>. However with the helper above it is not working, although the submenu is inheriting the class active, the main menu is closing.

Can you direct me ?

1 answer

2


I’d do it this way:

<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 in 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 submenu, you can do a validation between the 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 so, put on top of each module, in the building:

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

Then you do it:

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

As far as the main menu is concerned, do not submit.

  • 1

    It worked this way: Edit: Em caso de ser submenu, você pode fazer uma validação entre os módulos, if($this->uri->segment(1)=='categoria' or $this->uri->segment(1)=='categoriaB' or $this->uri->segment(1)=='categoriaC') echo "active"; Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.