Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Filter in GridView with Yii2

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

like image 613
Francis Avatar asked Nov 09 '15 12:11

Francis


4 Answers

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 ..

like image 65
Midhun Avatar answered Oct 14 '22 07:10

Midhun


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

like image 22
Liz Avatar answered Oct 14 '22 06:10

Liz


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')

like image 2
pedro casas Avatar answered Oct 14 '22 05:10

pedro casas


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,
    ],
]);
like image 1
Touqeer Shafi Avatar answered Oct 14 '22 05:10

Touqeer Shafi