Reputation: 289
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
Upvotes: 5
Views: 15788
Reputation: 842
If your default gridview filter value comes from an expression, call its property just before $this->load($params)
in the Search's model method:
class ContractSearch extends Contract
{
...
public $year;
public function search($params)
{
$this->year = date('Y');
$this->load($params);
}
...
}
Upvotes: 0
Reputation: 11
much more easier!
class YourModelSearch extends \app\models\YourModel
{
public $status_id = 'published';
...
Upvotes: 1
Reputation: 159
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')
Upvotes: 3
Reputation: 575
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
Upvotes: 7
Reputation: 3988
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 ..
Upvotes: 11
Reputation: 384
A bit late, but only to keep a record on SO.
One way of setting the allowed filters in a Yii2 GridView
widget is to use its filterModel
object's rules
function to return the wanted filtering fields set with the save
attributes. So you can remove from this list all the unwanted filters not needed to be displayed in the GridView
.
You can then customize the ActiveDataProvider
query under the search
function of the filterModel
to properly build the requested filtered data.
Upvotes: 0
Reputation: 5264
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,
],
]);
Upvotes: 1