0

I have an index.php file generated with the Gii Code Generator. It has a GridView with the data of a table of my database. It has 536000 rows so it moves very slow. Even sometimes exceeds 30 seconds so the page is not loaded at all.

I would like to start my index file (http://localhost:8080/persons/index.php) but with an empty GridView (or without the GridView) and it can be filled when the user uses filters of a _search.php file.

My index.php file:

<? Pjax::begin() ?>
<? echo $this->render('_search', ['model' => $searchModel]) ?>

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'id',
        'name',
        'details',
    ],
]) ?>
<? Pjax::end() ?>

My _search.php file:

<?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
    'options' => [
        'data-pjax' => 1
    ],
]) ?>

<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'datails') ?>

<div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>

<? ActiveForm::end() ?>

My personsController.php file:

public function actionIndex()
{
    $searchModel = new PersonsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}
Roby Sottini
  • 2,117
  • 6
  • 48
  • 88
  • You can try to move this chunk of code to another `php` file and create a function in controller, which will return on ajax or post request rendered gridview. then embed in right place. this function should be called on document ready state. But it is in theory :) just a try. – Serghei Leonenco Jun 06 '20 at 05:03
  • Or like addition example i would pass (if this is not a ajax or post request or is equal to null) an array with properties of limit of the modal like equal to 10, so you basically display 10 records from the begging, but the rest what ever customer request. – Serghei Leonenco Jun 06 '20 at 05:12
  • 1
    are you using the gridview filters too along with the search form ? – Muhammad Omer Aslam Jun 06 '20 at 07:09
  • @MuhammadOmerAslam: Yes. Is it a bad idea? – Roby Sottini Jun 06 '20 at 20:15

1 Answers1

1

You can use query parameter, let's say, "limit" and feed it on the index page and on other. And than use it in DataProvider construct in PersonsSearch model. Or use "pagination" property.

public $limit = null;

public function search()
{

    $query = Person::find();

    if ($this->limit) {
        $query->limit($this-limit);
    }

    $provider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 20,
        ],
    ]);

    return $provider;
}
Alex
  • 21
  • 5