Save filters from GridView to session, keep filter state between pages.
- Very flexible. Separate setting and getting.
- Setting via behavior.
- Determines uniqueness by action's route and a customizable ID.
To install, either run
php composer.phar require --prefer-dist thrieu/yii2-grid-view-state "dev-master"
or add
"thrieu/yii2-grid-view-state": "dev-master"
to the require section of your composer.json
file and then run composer update
.
Create \app\widgets\Gridview.php
Extend GridView
class, simply implement FilterStateInterface
and FilterStateTrait
.
namespace \app\widgets;
use thrieu\grid\FilterStateInterface;
use thrieu\grid\FilterStateTrait;
class GridView extends \yii\grid\GridView implements FilterStateInterface {
use FilterStateTrait;
}
Attach the filter behavior to your GridView
widget.
use \app\widgets\Gridview;
...
GridView::widget([
...
'as filterBehavior' => \thrieu\grid\FilterStateBehavior::className(),
...
]);
Update \app\models\xxxSearch.php
To get the params which is merged with GridView state params and GET query params, and then set it to filter model and DataProvider
.
// DataProvider
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'params' => \app\widgets\GridView::getMergedFilterStateParams(),
],
'sort' => [
'params' => \app\widgets\GridView::getMergedFilterStateParams(),
],
]);
// Filter model
$this->load(\app\widgets\GridView::getMergedFilterStateParams());
Enjoy it.
Update \app\controllers\xxxController.php
Add ClearFilterStateBehavior
to behaviors()
of your controller.
public function behaviors()
{
return [
...
'clearFilterState' => \thrieu\grid\ClearFilterStateBehavior::className(),
...
];
}
And then add a form to your frontend page.
$form = Html::beginForm();
$form .= Html::hiddenInput('clear-state', '1');
$form .= Html::hiddenInput('redirect-to', '');
$form .= Button::widget([
'label' => Yii::t('app', 'Reset filter'),
]);
$form .= Html::endForm();
echo $form;
$filterData = GridView::getMergedFilterStateParams(null, null, 'delivery/cmd-delivery/index');
$model->load($filterData);
for gridview set usepreview = true
use \app\widgets\Gridview;
...
GridView::widget([
...
'as filterBehavior' => \thrieu\grid\FilterStateBehavior::class,
'usePrevNext' => true,
...
]);
for prev next buttons easy get previousa nd next model keys values
$prevNext = new PrevNextPage('cars/in-way/index');
if ($prevId = $prevNext->getPrevPage($model->id)) {
echo ThButton::widget([
'tooltip' => Yii::t('blankonthema', 'Previous record'),
'link' => [
'view',
'id' => $prevId,
],
'icon' => 'arrow-left',
'type' => ThButton::TYPE_DEFAULT
]);
}
if ($nextId = $prevNext->getNextPage($this->id)) {
echo ThButton::widget([
'tooltip' => Yii::t('blankonthema', 'Next record'),
'link' => [
'view',
'id' => $nextId,
],
'icon' => 'arrow-right',
'type' => ThButton::TYPE_DEFAULT
]);
}