Over-engineering for a simple system |
I really feel like I'm making this harder than it needs to be, but I can't see a simpler solution... hopefully someone can help me on this.
I have a navigation sidebar (Bootstrap 4), and I want to set the navigation items as active when going to them. Some items are in a secondary drop-down menu, and so I need to set the parent as active (menu-open) also. So first I need to get the URI segments, and then determine if that path is under the parent and set both parent and child as active. My problem with the method I'm using is that it uses a lot of PHP in the view, instead of separating the logic to the controller. But I don't see a simple method of defining both paths in a controller, and then relaying that to the view for each of the menu items to be active or not active. Admin Controller PHP Code: <?php Sidebar View PHP Code: <nav class="mt-2">
Perhaps not the best way to tackle your problem, but here is how I "check" the current page for the navigation.
You could load the current controller or method with something service('router')->controllerName(); then pass that to the navigation view as your current controller. Then all you need is PHP Code: class="nav-item <?= $Controller == 'admin' ? 'active' : ''; ?>" Same can be done for ->methodName() if needed too. This might decrease some php code in your view.
This might help you out, you may need to modify it a bit.
Highlight current page menu item with jQuery Should point you in the right direction. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(04-20-2020, 01:24 AM)berendbotje91 Wrote: Perhaps not the best way to tackle your problem, but here is how I "check" the current page for the navigation. Thanks, but I'm not sure what you're doing different here than what I already have? PHP Code: class="nav-link <?= (uri_string() === 'admin/profile') ? 'active' : ''; ?>" (04-20-2020, 04:04 AM)InsiteFX Wrote: This might help you out, you may need to modify it a bit.I don't know why I didn't think of JQuery for this... thanks!
If you scroll to the bottom of the page maybe the middle he shows you how to just what you are asking.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |