Paging and filter in Yii2 Gridview

I used the CRUD generator in Yii2 and it generated the following code for my controller actionIndex...

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

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

I am trying to do two things for this code by default:

1) Set the page size so that the grid display displayed on it displays only 10 lines

2) Change it $searchModelso that it returns only records where the status column in the table corresponds to some several values ​​(IN operator) ... or better yet, all records that do not match the given value.

For # 1, I see many examples for setting "pagination" when used ActiveDataProvider, but not for search(). This code did not work for me ...

$dataProvider = $searchModel->search(
    Yii::$app->request->queryParams, ['pagination' => [ 'pageSize' => 10 ]]
);

For # 2, I know that we can filter by declaring a new LeadSearch object as ...

$searchModel = new LeadSearch([ 'status' => 'open' ]);

... - ...

$searchModel = new LeadSearch([ 'status' => ['open', 'pending'] ]);
+4
3

pagination ActiveDataProvider , where andWhere $query .

$query = modalName::find()->andWhere([ 'status' => ['open', 'pending'] ]);

$dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [ 'pageSize' => 10 ],
        ]);
+12
    $searchModel = new PersonSearch();
    $dataProvider = $searchModel->search(
    Yii::$app->request->queryParams);
    $dataProvider->pagination = ['pageSize' => 10,];
+2

enter image description here

$dataProvider->pagination = ['pageSize' => 10];
-4
source

Source: https://habr.com/ru/post/1606605/


All Articles