Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 field filter not showing on GridView

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.

like image 988
Carlos Marmolejo Avatar asked Feb 11 '16 05:02

Carlos Marmolejo


People also ask

How to use filters in GridView?

Filters are mainly text input but in general they can be any type of control and we can customize them as much as we want. Filters can be activated by filling out the GridView widget property filterModel with an instance of the model class and automatically a new row will be created below the header, containing working text inputs.

How do I configure the columns of the grid view?

The columns of the grid table are configured in terms of yii\grid\Column classes, which are configured via $columns. The look and feel of a grid view can be customized using the large amount of properties. For more details and usage information on GridView, see the guide article on data widgets.

How to add full name column in the grid view?

Voila, your fullName column in the grid view should be available for sort and filtering. STEP 1: Ensure your Person model has a relation defined to the Country model. You can also implement a getter for CountryName. STEP 2: Add an attribute countryName to your model PersonSearch and configure your rules.

How to create a behavior object in Yii?

an object configuration array that will be passed to Yii::createObject () to create the behavior object. Attaches a list of behaviors to the component. Each behavior is indexed by its name and should be a yii\base\Behavior object, a string specifying the behavior class, or an configuration array for creating the behavior.


1 Answers

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;

like image 115
ScaisEdge Avatar answered Oct 13 '22 06:10

ScaisEdge