Laravel-admin: 左侧的导航不能更新状态

Created on 10 Dec 2016  ·  8Comments  ·  Source: z-song/laravel-admin

选中某条导航,那一条应该变为激活状态,其他的应该变成非激活状态。并且刷新之后同样能正常显示

All 8 comments

@yzy1197988245 試試這段能不能解決你的問題

    $(function(){
        $('.sidebar-menu li:not(.treeview) a').each(function(){
            $(this).on('click', function () {
                $(this).parent().siblings().removeClass('active')
                $(this).parent().siblings('.treeview').children('ul.treeview-menu.menu-open').css( "display", "none" );
                $(this).parent().addClass('active');
            });
        });
    });

@yang5664 谢谢,已经加入了你的代码

@yang5664 @z-song 这样写或者会更好:

    $(function(){
        $('.sidebar-menu li:not(.treeview) > a').on('click', function(){
            $(this).parent().addClass('active').siblings('.treeview.active').find('> a').trigger('click');
        });
    });

这样写会在展开折叠的菜单会看到之前有激活过的菜单一闪而过。

下面这种写法更好:

    $(function(){
        $('.sidebar-menu li:not(.treeview) > a').on('click', function(){
            var $treeviewActive = $(this).parent().addClass('active').siblings('.treeview.active');
            $treeviewActive.find('> a').trigger('click');
            $treeviewActive.find('li').removeClass('active');
        });
    });

@edwinhuish 單一選單沒有清除"active",要加上一段

$(this).parent().siblings().removeClass('active'); <== 清除其它active

screen shot 2017-02-16 at 11 09 34 pm

@yang5664 疏忽了,修正:

    $(function(){
        $('.sidebar-menu li:not(.treeview) > a').on('click', function(){
            var $parent = $(this).parent().addClass('active');
            $parent.siblings('.treeview.active').find('> a').trigger('click');
            $parent.siblings().removeClass('active').find('li').removeClass('active');
        });
    });

@yang5664 @edwinhuish 已经修改好了

@yang5664 @z-song 另外添加刷新后也active对应的菜单:

    $(function(){
        $('.sidebar-menu li:not(.treeview) > a').on('click', function(){
            var $parent = $(this).parent().addClass('active');
            $parent.siblings('.treeview.active').find('> a').trigger('click');
            $parent.siblings().removeClass('active').find('li').removeClass('active');
        });

        $(window).on('load', function(){
            $('.sidebar-menu a').each(function(){
                if(this.href === window.location.href){
                    $(this).parent().addClass('active')
                            .closest('.treeview-menu').addClass('.menu-open')
                            .closest('.treeview').addClass('active');
                }
            });
        });
    });

好像现在这个版本,刷新了就没有选中状态了。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qcol picture qcol  ·  3Comments

piian picture piian  ·  3Comments

clock1129 picture clock1129  ·  3Comments

joernroeder picture joernroeder  ·  3Comments

evans-kim picture evans-kim  ·  3Comments