Yii2 field filter not showing on GridView
11,236
Be sure you have a proper rulese function declare in CalendarioSearch eg:
public function rules()
{
return [
[['id_materia' ], 'integer'],
[['nombre_sucursal', 'materia' ], 'safe'],
];
}
and be sure you use the same name in filter and i class ..
in you filter you are using nombre_sucursal but in you class you are using $nombreSucursal;
Author by
Carlos Marmolejo
Updated on June 13, 2022Comments
-
Carlos Marmolejo almost 2 years
I cannot make my GridView filter by any field, the Grid displays ok, but my filter section appears in blank, this is the GridView declaration:
GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ [ 'attribute'=>'nombre_sucursal', 'value'=>'sucursal.nombre_sucursal', 'filter' => ArrayHelper::map(Sucursal::find()->asArray()->all(), 'id_sucursal', 'nombre_sucursal'), 'label'=>'Sucursal' ], [ 'attribute'=>'nombre_materia', 'value'=>'materia.nombre_materia', 'label'=>'Materia' ], 'fecha:datetime', ], 'export'=>false ]);?>
this is the declarations of my searchModel and dataProvider:
$searchModel = new CalendarioSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
And my search model is this:
class CalendarioSearch extends Model { /* your calculated attribute */ public $nombre_sucursal; public $nombre_materia; /* setup rules */ public function rules() { return [ /* your other rules */ [['nombre_sucursal'], 'safe'], [['nombre_materia'], 'safe'] ]; } public function search($params) { $query = Calendario::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 20, ], 'sort' => [ 'defaultOrder' => ['fecha'=>SORT_DESC], 'attributes' => [ 'nombre_sucursal' => [ 'asc' => [ 'id_sucursal' => SORT_ASC, 'fecha' => SORT_DESC ], 'desc' => [ 'id_sucursal' => SORT_DESC, 'fecha' => SORT_DESC ], ], 'nombre_materia' => [ 'asc' => [ 'id_materia' => SORT_ASC, 'fecha' => SORT_DESC ], 'desc' => [ 'id_materia' => SORT_DESC, 'fecha' => SORT_DESC ], ], 'fecha' => [ 'asc' => [ 'fecha' => SORT_ASC, ], 'desc' => [ 'fecha' => SORT_DESC, ], ], ], ] ]); if (!($this->load($params) && $this->validate())) { $query->joinWith(['sucursal']); $query->joinWith(['materia']); return $dataProvider; } $query->joinWith(['sucursal' => function ($q) { $q->where('c_sucursal.id_sucursal LIKE "%' . $this->nombre_sucursal . '%"'); }]); $query->joinWith(['materia' => function ($q) { $q->where('c_materia.id_materia LIKE "%' . $this->nombre_materia . '%"'); }]); return $dataProvider; } }
The document that I followed was this.
-
Carlos Marmolejo about 8 yearsI just update the question with the rules that I have in my search model.
-
ScaisEdge about 8 yearsI have update the answer suggesting to use the same proper name .. in the related part of code .
-
Carlos Marmolejo about 8 yearsThank you this work for me, with this I can see the filters in the grid and after that I have to make some other corrections in my code to make it work. I update the question with the other corrections. Thanks again.