Closed gb5256 closed 7 years ago
Hi! The correct namespace is marqu3s\behaviors and not frontend\models. Make sure to include the following line at the top of your model class where you are attaching the behavior:
use marqu3s\behaviors\ SaveGridPaginationBehavior
Let me know if all went fine.
Hi. thanks. That removed that error message. But now I get this one: Calling unknown method: frontend\controllers\SiteController::getGridPage()
The example is based on the case where you have a model class and a search model class, like Post and PostSearch. The behavior must be attached to the search model class and it will work.
Check the Yii 2 guide about model and search model: http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html
Look for the Filtering Data section.
Nice job marqu3s. All work fine. Two questions: Can I put default values for my filters? Wow can I reset the values of the filters to the original values? Thx.
@ric128 Thanks!
Implementing default values for the filters is up to you. Since the SaveGridFiltersBehavior attaches to a search model class, you could provide default values when instantiating the search model. The first time your user access a page with a grid, there will be no $_GET data. By checking if this is the scenario, you could set the default values accordingly. Something like this:
$searchModel = new PostSearch(); // this search model have the SaveGridFiltersBehavior attached to it.
$params = Yii::$app->request->queryParams;
if (empty($params)) {
$params['PostSearch']['status'] = 1;
}
$dataProvider = $searchModel->search($params);
The example above will set a default value of 1 to the status filter/attribute. It will work as if the filter of the status column was set to 1 (or active) by your user. Adapt as needed.
Now to reset the values of the filters, you could use a button on the page to do an ajax request with a control variable that tells the Controller to use the default values and return a new grid. Then replace the grid on the page with the new one.
all work fine... thx a lot.
Hi Marqu3s I think the question
if (empty ($ params))
is not appropriate to set the default values of the filters. Each time the app call
$ this-> redirect ([ 'index']);
the index does not receive parameters, therefore attempts to reset the filters again and does not retain the values of the session. Please strengthen me if I'm right or I'm doing something wrong. Thx.
Then we'll have to think in something else. Feel free to send a push request with your solution if you think you came up with something that might be useful to others.
Hi, if I use it as shown in the documentation, then I get his error:
PHP Fatal Error – yii\base\ErrorException Class 'frontend\models\SaveGridPaginationBehavior' not found
Do I need to register this behavious in the main.config ?