Alexander Sedov2017-07-14 15:05:05
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)
Maxim Timofeev, 2017-07-14

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:
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

Boris Korobkov, 2017-07-14

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.

artursharipov, 2020-02-21

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

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