8
votes

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.

1
Did you set up rules in your search model?Dency G B
Where does addCondition come from? This is not a regular Model class, right?robsch
I just update the question with the full search model ʎɔuǝp. Robsch what do you mean with a regular model I extended from Model? is this ok? or I have to extend from a specific searchmodel class?Carlos Marmolejo

1 Answers

8
votes

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;