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.
rules
in your search model? – Dency G B