H
H
Historian1112018-11-12 20:38:05
Yii
Historian111, 2018-11-12 20:38:05

How to insert the active class into the site menu during foreach?

Good afternoon. I have been practicing a little in creating a site, and there was a small problem with the menu of this site.
When a button is clicked, the active class should be added, which highlights the pressed button.
But I want to completely add the menu to the database, for the convenience of changing or adding new items. And here the problem began, I get data from the database,

I go through the cycle and output in the right places
<?php if(!empty($navLinks)):?>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <?php foreach ($navLinks as $navLink):?>
                <li><a href="<?= $navLink->href?>" class="scroll-to"><?= $navLink->name?></a></li>
                <?php endforeach;?>
            </ul>
        </div>
        <?php endif;?>

Everything is displayed cool, only the first button is always selected, the active does not stick to the rest when switching. I develop on Yii2. Has anyone come across this issue, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-11-12
@Historian111

Good evening.
Use the widgets provided by yii2, everything will work.
For example yii\widgets\Menu;
You get all the necessary menu items from the database, form them into an array and "feed" them into the widget.

Menu::widget([
    'items' => $items
])

You can also use NavBar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question