user5883126
user5883126

Reputation:

Reload only gridview in yii2

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.

Upvotes: 2

Views: 9948

Answers (2)

Oliver1094
Oliver1094

Reputation: 21

Try

$("#idyourgrid").yiiGridView("applyFilter");

Upvotes: 2

Fabrizio Caldarelli
Fabrizio Caldarelli

Reputation: 3008

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'})
});

Upvotes: 6

Related Questions