How to disable pjax on a specific link inside of a GridView?


(tomsea) #1

Hi, according to the docs:

You may disable pjax for a specific link inside the container by adding data-pjax="0" attribute to this link.

Source

However, the “data-pjax” attribute is not being rendered by the Html::a helper for a simple link column in a GridView widget. This is my code:

    <?php Pjax::begin(); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute'=>'name',
                'format'=>'html',
                'value' => function ($data) {
                    return Html::a(Html::encode($data->name), ['view', 'id' => $data->id], ['data-pjax'=>0]);
                },
            ],
        ],
    ]); ?>
    <?php Pjax::end(); ?>

Am I missing something? If I change the third argument of Html::a to be:

['class'=>'abc']

It renders the attribute properly, so I’m lost as to why it’s not rendering the required custom attribute to disable pjax on that link. In fact, no custom html tags are being rendered. Any ideas will be appreciated. Thanks!


(tomsea) #2

The problem was:

'format'=>'html',

because “the value is purified using HtmlPurifier to avoid XSS attacks”, so the data-pjax attribute was being filtered out. The solution was to use “raw” format to avoid any purification:

'format'=>'raw',

However, this will be insecure if some of the HTML comes from user input. In that case, perhaps making an exception for the data-pjax attribute (whitelisting) may be possible by configuring the HTMLPurifier helper, but in my case it was just a simple link so it’s solved for me. Hope it helps someone in the future.