fortm
fortm

Reputation: 4208

yii2 listview next page without changing url

Yii2 listview is missing $.fn.yiilistview.update from yii1. Even more, it is dependent on pjax for basic requirements and thus creates all kinds of issues with nested pjax , timeouts etc for simple pagination with listView.

A GET request to controller is rendered by javascript into an HTML ( jSON returned from controller containing listview html ) . Since URL is not changed in this process, I did not use pjax for avoiding complexity and made simple ajax call and based on data returned on success will update container. ( like pjax does only manually using ajax without changing url)

now when I do next page, then URL is updated and also page full refreshes. Initially it was not working as well but once I added an IF check on page GET param inside controller and instead of returning renderAjax output in JSON, would directly return HTML now

How can we avoid updating URL with yii2 listview next page without using pjax and make it work seeemlesly with controller and javascript written in fashion to emulate pjax in manual ajax calls as stated above ?

Controller :

if($pageParam == null) {
    $page =  $this->renderAjax("_list", [
                        "dataProvider" => $dataProvider
             ]) ;

    Yii::$app->response->format = 'json';
    return array("error"=>false,"page"=>$page);
} else {
    return $this->renderAjax("_list", [
                        "dataProvider" => $dataProvider
             ]) ;
}

List View :

<?php if($dataProvider != null) : ?>
    <?= 
    ListView::widget([
        'id'=>'itemList',
        'dataProvider' => $dataProvider,
        'itemOptions' => ['class' => 'item'],
        'itemView' => '_dataListView',
        'summary'=>'',
        'emptyText'=>$emptyPage         
    ]) 
    ?>
<?php endif; ?>

js :

ajax success GET callback :

success     : function(data) {
                    if(!data.error) {
                        $("#list-container").html(data.page);

listcontainer itself resides in parent view for _list

Upvotes: 4

Views: 4664

Answers (2)

e-frank
e-frank

Reputation: 749

you can configure pjax widget like this:

    <? Pjax::begin(['enablePushState' => false]) ?>
...
    <? Pjax::end() ?>

you might also be interested in the 'enableReplaceState' option. doc here

Upvotes: 8

fortm
fortm

Reputation: 4208

well, figured this out, trick was to not do pjax:begin / pjax:end calls in PHP and instead take control in javscript as any way I was manually replacing HTML using JSON returned from yii.

I could not figure a way without pjax - so here is solution on ajax:success

$("#list-container").html(data.page);
$(document).pjax('a', '#list-container', {push:false});

Upvotes: 0

Related Questions