Save filters from GridView to session, keep the filter state between pages.
- Very flexible. Separate setting and getting.
- Setting via behavior.
- Determines the uniqueness by the action route and a customizable ID.
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.
GridView::widget([
...
'as filterBehavior' => \thrieu\grid\FilterStateBehavior::className(),
...
]);
Get the params which merging the GridView state params with GET query params and set it to the filter model and the DataProvider.
// Filter model
$model->load(GridView::getMergedFilterStateParams());
// DataProvider
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'params' => GridView::getMergedFilterStateParams(),
],
'sort' => [
'attributes' => $attributeOrders,
'params' => GridView::getMergedFilterStateParams(),
],
]);
The functionality of clearing state would be added in the nearly future.