Reputation: 75
trying to enable both pagination and search filters in a gridview. I understand I'm overwriting dataProvider in the following code, but I am not sure how I could otherwise do it. The problem, of course, is that currently if I change the page or do a filter search, the pagination disappears.
public function actionLibrary()
{
$query = FileEntry::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
$searchModel = new FileEntrySearch();
if(Yii::$app->request->isAjax)
{
$dataProvider = $searchModel->search(Yii::$app->request->get());
}
return $this->render('library', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
Upvotes: 1
Views: 1227
Reputation: 22174
You can change pagination configuration after creating data provider:
public function actionLibrary() {
$searchModel = new FileEntrySearch();
$dataProvider = $searchModel->search(Yii::$app->request->get());
$dataProvider->pagination->pageSize = 10;
return $this->render('library', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
Upvotes: 0
Reputation: 91
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
This assumes, that your $searchModel
is already an instance of an ActiveDataProvider, which I assume you defined in your FileEntrySearch model. So your action could look something like this:
public function actionLibrary()
{
$searchModel = new FileEntrySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('library', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
and your FileEntrySearch:
class FileEntrySearch extends FileEntry
{
// [...] rules, scenarios etc.
public function search($params)
{
$query = FileEntry::find();
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
// you can add sorting here
$this->load($params);
// you can define your filters here
return $dataProvider; // this will become your $searchModel
}
}
No dataProvider overriding this way.
Upvotes: 2