D
D
Dmitry Petrik2015-01-29 09:57:38
css
Dmitry Petrik, 2015-01-29 09:57:38

Yii2. How to remove classes generated by Yii in widgets?

I am using the NavBar widget. Code like this:

<?php NavBar::begin([
             'renderInnerContainer' => false,
             'options' => [
                      'renderInnerContainer' => false,
                      'tag' => 'div',
                      'class' => 'row',
             ],
 ]); ?>
...
<?php NavBar::end(); ?>

As a result, I get this HTML:
<div id="w0" class="row navbar" role="navigation">
...
</div>

In the widget settings I registered the class "row", and at the output I get "row navbar". How can I get rid of the "navbar" class?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan, 2015-01-30
@Flexo

Redefine the class with your own and business.

class NavBar extends \yii\bootstrap\NavBar
{
    public function init()
    {
        parent::init();
        Html::removeCssClass($this->options, 'navbar');
    }
}

M
melnikov_m, 2015-01-29
@melnikov_m

I really don't recommend doing that. Six months later, you will forget that something has changed there, and when you update the widget, everything will be overwritten, if you really want to delete the class, do it through jQuery.
$('#w0').removeClass('navbar');

S
SaGrok, 2015-10-27
@SaGrok

Or maybe just use yii\widgets\Menu instead of yii\bootstrap\NavBar?

D
Dmitry Petrik, 2015-01-29
@Flexo

In general, I didn’t come up with anything better than getting into the source code of the widget and commenting out the line adding this class there.
The question is valid. I would like a more elegant solution.
// Update
It's better not to comment the line, but rewrite it like this:

if (!isset($this->options['class'])){
            Html::addCssClass($this->options, 'navbar');
}

If you just comment out the line, then in the case when the class is not explicitly set, an exception will be thrown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question