I don't know how to set the filter default of GridView. It's mean when page loaded, it's will load the filter with specific condition that I've set.
Any idea for this? Thanks
A simple way to do this is by using the search model .
I am using Default Gii generated code to explain the ways
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Say you want a dynamic filter when the page is loaded
use the link as
../index.php?r=user/index&UserSearch[id]=7
This will add a filter where id = 7 ie in my case since id is the primary key only one user will be listed
Say if you want always apply a filter without showing anything in the url
public function actionIndex()
{
$searchModel = new UserSearch();
$searchModel->name = 'mid';
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This will create a filter where user's name has string 'mid'
if you want more advanced filters
you can edit the search() function in the UserSearch Class there the query used to populate the data and ActiveDataProvider will be available to you . say you do't want to list users who are inactive .
public function search($params)
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
$query->andFilterWhere(['active_status' => 1]);
....
this method will provide you with limitless ways to filter your results .. Hope this helps ..
I had the same problem and it worked for me
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andFilterWhere(['status'=>1]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This helps performing the filter for action is needed and for all, in my case I needed it alone in an environment
public function actionIndex()
{
$searchModel = new UserSearch();
// Filtro por Defecto y Reflejado en Formulario de Filtrado en Grid
$params = Yii::$app->request->queryParams;
if (!isset($params['UserSearch'])) {
$params['UserSearch']['status']=1;
}
$dataProvider = $searchModel->search($params);
// -----------------------------------------------------------
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
NOTA: Usar (!isset($params['UserSearch'])) para que solo se aplique como búsqueda por defecto ('Si no se ha definido ninguna condición de filtrado')
NOTE: Use (**! Isset ($ params ['UserSearch']) **) so that it only applies as a default search ('If no filter condition has been defined')
Yii2 ActiveDataProvider it self need a query builder, means you can filter your results when passing it the query object eg:
$query = Post::find()->where['status' => 'published'];
// Todo and more conditions with $query object
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With