Reload only gridview in yii2

user5883126 picture user5883126 · Feb 1, 2017 · Viewed 7.9k times · Source

How can I, reload only grid-view on on change event of drop-down in Yii2?

I know that it can be done via pjax but not sure where and how to use the code.

I am using Ajax request for communicating with controller. Here is the ajax code:-

function loadGrid(level) { 
    alert(level); 
    $.ajax({ 
        type: 'GET', 
        url: 'index.php?r=villagedata/level', 
        data: {level:level}, 
        success: function(data) 
        { 
            alert("Success"); 
            $.pjax({container: '#myview'}); 
        } 
    }); 
} 

I wan't my grid to reload when the Ajax Request returns success message.

Thank You.

Answer

Fabrizio Caldarelli picture Fabrizio Caldarelli · Feb 1, 2017

Exactly, using Pjax.

use yii\grid\GridView;
use yii\widgets\Pjax;

<?php Pjax::begin(['id' => 'pjax-grid-view']); ?>    
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            '...'
        ],
    ]); ?>
<?php Pjax::end(); ?>

and jQuery to detect drop down change.

If dropdown has "dropdown" id,

$('#dropdown').on('change', function(ev) {
     $.pjax({container: '#pjax-grid-view'})
});