Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 listview next page without changing url

Tags:

yii2

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

like image 340
fortm Avatar asked Feb 04 '23 18:02

fortm


1 Answers

you can configure pjax widget like this:

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

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

like image 181
e-frank Avatar answered May 12 '23 05:05

e-frank