Yii2字段过滤器未在GridView上显示

时间:2016-02-11 05:38:57

标签: gridview filter yii2 field

我无法通过任何字段制作我的GridView过滤器,Grid显示正常,但我的过滤器部分显示为空白,这是GridView声明:

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
        ]);?>

这是我的searchModel和dataProvider的声明:

$searchModel = new CalendarioSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

我的搜索模型是:

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;
    }
}

我关注的文件是this

1 个答案:

答案 0 :(得分:3)

请确保您在CalendarioSearch中声明了正确的规则函数,例如:

public function rules()
{
    return [
        [['id_materia' ], 'integer'],
        [['nombre_sucursal', 'materia' ], 'safe'],
    ];
}

并确保在filter和i class中使用相同的名称..

在你过滤你正在使用nombre_sucursal,但在你的班级你使用$ nombreSucursal;