A
A
Alexander Sedov2017-07-14 15:05:05
Yii
Alexander Sedov, 2017-07-14 15:05:05

Ajax loading in GridView Yii2?

Good afternoon. In a Yii2 project, Gridview is used to display data.
Actually the question is: how in Yii2 to make it so that instead of pagination, the data was loaded into the table when scrolling?
Prompt the decision or kick in the necessary direction.
Google gives the wrong thing :( Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2017-07-14
@ninetwgb

GridView has nothing to do with it, it just builds a table based on the data you feed it, and you feed it data in an ActiveDataProvider that has a pagination that contains the yii\data\Pagination object. It contains data on the current number of records, so we just wrap everything in pjax and write js that will scroll up to a certain point, launch pjax already at a new url, with a new limit.
There is a ready-made solution, I have not tried it, but there are a lot of stars, so it should work:
https://github.com/kop/yii2-scroll-pager
Google does not give you results, because you are looking for gridView , but it has nothing to do with this task , look for "yii2 scroll pager" or something similar

B
Boris Korobkov, 2017-07-14
@BorisKorobkov

Google "pjax".
How to use it when clicking on a pagination - for example, https://habrahabr.ru/post/225823/
For autoloading when scrolling, you will have to emulate a click on the desired pagination when scrolling.

A
artursharipov, 2020-02-21
@artursharipov

I have your example with only the "Show more" button. Tweak my code for you
1) Hide the pagination
2) This is under the GridView

<?php
echo Html::a('Показать ещё', [Url::to('').'#ddd'], [
   class' => 'btn btn-default',
   'data' => [
      'method' => 'post',
      'params' => [
         'more' => $more,
      ],
      'pjax' => 1,
   ],
   'id' => 'ddd',
   ]);
?>

3) It's in the controller
if($more = (int)Yii::$app->request->post()['more']){
    $dataProvider->pagination->pageSize += $more;
}

return $this->render('index',[
   /**/
   'more' => $more > 0 ? $more + 10 : 10,
]);

The whole point is this, initially pageSize is the default, and the button is to show another post with the more parameter, which will increase by 10 each time it is clicked. Url::to#dd to save the filter and scroll to the button.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question