Mr Goobri Mr Goobri - 3 months ago 21
jQuery Question

Yii2 - swap ListView for GridView on jQuery.click()

I have a ListView, which I create myself. And I have a GridView, using

kartik\grid\GridView
.

Both work fine on their corresponding pages. They use the same parameters, such as
$dataProvider
and
$searchModel
.

<div id="grid">
<?= GridView::widget([....]); ?>
</div>


And

<div id="list">
<?= ListView::widget([....]); ?>
</div>


The problem is, when I put them on the same page, the second one loses its interactivity - things like pagination, for example. I have a jQuery.click() button to swap out the GridView for the ListView but it doesn't work.

$("#presentation-switcher").click(function() {
$("#data-view").load("_list.php");
});


I've experimented with different ids, different dataprovides etc. but to no avail. Perhaps I am taking the wrong approach? Any ideas?

Answer

Set a new page of item view like _list_item:

<?= 
ListView::widget([
    'dataProvider' => $listDataProvider,
    'options' => [
        'tag' => 'div',
        'class' => 'list-wrapper',
        'id' => 'list-wrapper',
    ],
    'layout' => "{pager}\n{items}\n{summary}",
    'itemView' => '_list_item',
]);
?>

More Detail

Comments