Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu with sub menu does not stay unfolded #795

Closed
rabol opened this issue Jan 9, 2021 · 13 comments
Closed

Menu with sub menu does not stay unfolded #795

rabol opened this issue Jan 9, 2021 · 13 comments

Comments

@rabol
Copy link

rabol commented Jan 9, 2021

Question Answer
Issue or Enhancement Question ?
Laravel Version 8.21.0
Project Version 3.5.1

Current Behavior

When selecting a submenu item, the menu is not staying un folded and selected

Expected Behavior

That the menu is unfolded and the subitem selected

Steps to Reproduce

have this config:

'menu' => [
        [
            'text' => 'search',
            'search' => true,
            'topnav' => true,
        ],
        [
            'key' => 'admin_dashboard',
            'text' => 'Dashboard',
            'url' => 'admin',
            'icon' => 'fas fa-fw fa-tachometer-alt nav-icon'
        ],
        [
            'key' => 'admin_user_management',
            'text'    => 'User management',
            'icon'    => 'fa-fw nav-icon fas fa-users',
            'submenu' => [
                [
                    'text' => 'Permission',
                    'route'  => 'admin.permissions.index',
                ],
                [
                    'text' => 'Roles',
                    'route'  => 'admin.roles.index',
                ],
            ],
        ],
]

then on the page select Permissions or Roles

@dfsmania
Copy link
Collaborator

dfsmania commented Jan 9, 2021

Hi @rabol , can you share the filters configuration array and the complete urls that are resolved for the admin.permissions.index and admin.roles.index routes?

@rabol
Copy link
Author

rabol commented Jan 9, 2021

I did not touch the standard:

 'filters' => [
        JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\HrefFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\SearchFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\ActiveFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\ClassesFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter::class,
        JeroenNoten\LaravelAdminLte\Menu\Filters\DataFilter::class,
    ],
<a class="nav-link  "
       href="http://laravel.test/admin/permissions"        >
        <i class="far fa-fw fa-circle "></i>
        <p>
            Permission
                    </p>
</a>

<a class="nav-link  "
       href="http://laravel.test/admin/roles"        >
        <i class="far fa-fw fa-circle "></i>
        <p>
            Roles
                    </p>
</a>

@dfsmania
Copy link
Collaborator

dfsmania commented Jan 9, 2021

@rabol All appears to be ok. When you press the link, the url assigned to that link is opened on the browser, i.e, what you see on the browser's address bar? Is the same url assigned to the link? Also, is this a fresh installation of the package?

@rabol
Copy link
Author

rabol commented Jan 9, 2021

Yes, in the address bar it's the same url
Yes it's a fresh installation

@rabol
Copy link
Author

rabol commented Jan 9, 2021

if i change to this:

'url' => 'admin/roles',

instead of this

'route' => 'admin.roles.index',

it works

@rabol
Copy link
Author

rabol commented Jan 9, 2021

here is my route definition from web.php

Route::group(['prefix' => 'admin'], function () {
    Route::resource('permissions', App\Http\Controllers\Admin\PermissionController::class, ["as" => 'admin']);
});


Route::group(['prefix' => 'admin'], function () {
    Route::resource('roles', App\Http\Controllers\Admin\RoleController::class, ["as" => 'admin']);
});

@dfsmania
Copy link
Collaborator

dfsmania commented Jan 9, 2021

@rabol Thanks for the feedback, I have reproduced the problem in my environment, give me some time to check why isn't working on this particular situation...

@dfsmania
Copy link
Collaborator

dfsmania commented Jan 9, 2021

Hi @rabol , for the moment try next solution:

  1. Open the file vendor/jeroennoten/laravel-adminlte/src/Menu/Builder.php on your Laravel framework.
  2. Edit the file and change the method applyFilters() by the next one:
    /**
     * Apply all the available filters to a menu item.
     *
     * @param mixed $item A menu item
     * @return mixed A new item with all the filters applied
     */
    protected function applyFilters($item)
    {
        // Filters are only applied to array type menu items.
    
        if (! is_array($item)) {
            return $item;
        }
    
        // If the item is a submenu, transform all the submenu items first.
        // These items need to be transformed first because some of the submenu
        // filters (like the ActiveFilter) depends on these results.
    
        if (MenuItemHelper::isSubmenu($item)) {
            $item['submenu'] = $this->transformItems($item['submenu']);
        }
    
        // Now, apply all the filters on the item.
    
        foreach ($this->filters as $filter) {
    
            // If the item is not allowed to be shown, there is no sense to
            // continue applying the filters.
    
            if (! MenuItemHelper::isAllowed($item)) {
                return $item;
            }
    
            $item = $filter->transform($item);
        }
    
        return $item;
    }

Please, give me your feedback about this solution, and I will make a PR later to fix this issue.

@rabol
Copy link
Author

rabol commented Jan 9, 2021

seems to work for the index of the resource, but not for create, edit and show

e.g. http://laravel.test/admin/roles/1/edit collaps the menu

I added the new method:

compser dump-autoload
php artisan view:clear
php artisan cache:clear

anything else I need to do ?

@dfsmania
Copy link
Collaborator

dfsmania commented Jan 9, 2021

In that case, you will need to use the active attribute because your menu item needs to match multiple urls. For example:

[
    'key' => 'admin_user_management',
    'text'    => 'User management',
    'icon'    => 'fa-fw nav-icon fas fa-users',
    'submenu' => [
        [
            'text' => 'Permission',
            'route'  => 'admin.permissions.index',
            'active' => ['admin/permissions*'],
        ],
        [
            'text' => 'Roles',
            'route'  => 'admin.roles.index',
            'active' => ['admin/roles*'],
        ],
    ],
],

@rabol
Copy link
Author

rabol commented Jan 9, 2021

yes, the active did the trick - thanks a lot!

@dfsmania
Copy link
Collaborator

@rabol Thank you for reporting the issue, a fix will be available on the next versions.

@resslinger
Copy link
Collaborator

@rabol @Shidersz fixed with release v3.5.2

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants