spatie / menu

Html menu generator

Home Page:https://freek.dev/414-a-modern-package-to-generate-html-menus

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Active menu item is not hightliting

Fakhrulzaki94 opened this issue · comments

Hello,

I have a problem where my active menu item is not being set properly

this is my menu on http://localhost/geofence/assign :

image

this is my routes

| URI                                                       | Name                 |
| geofence                                                  | geofence.index       |
| geofence/assign                                           | geofence.assign      |
| geofence/show                                             | geofence.show        |

this my code

$this->createSubMenu('Geofence', 'fa fa-globe')
              ->routeIfCan(['view', Geofence::class],'geofence.index','<i class="fa fa-pencil-square-o"></i> <span>CRUD</span></a>')
              ->route('geofence.assign','<i class="fa fa-street-view"></i> <span>Assign</span></a>')
              ->route('geofence.show','<i class="fa fa-list"></i> <span>Contacts List</span></a>')
              ->setActiveFromRequest();

protected function createSubMenu($text = '', $iconClass =''){
    return Menu::new()->wrap('li', ['class' => 'treeview'])
              ->prepend('
                <a href="#">
                  <i class="'.$iconClass.'"></i>
                  <span>'. $text .'</span>
                  <span class="pull-right-container">
                  <i class="fa fa-angle-left pull-right"></i>
                  </span>
                </a>
              ')
              ->addClass('treeview-menu');
}

What would you expect to see in the rendered example?

Also, your example has 4 links but you only shared 2 routes. Could you be a bit more specific about what links to what?

There are 3 links, the "Geofence" is a treemenu. The problem is when i click submenu inside geofence menu, CRUD menu is hightliting.

That's because they share the same "root" in the url. You can declare the root in setActiveFromRequest to avoid this situation.

$this->createSubMenu('Geofence', 'fa fa-globe')
    // ...
    ->setActiveFromRequest('geofence');

I think this should do the trick.

Thank you, it work when i changed the route name 😄 👍