Reputation: 491
I have two tables "attendance" with attributes id
, status
, date
and "staff". staff_id
is used as foreign key in attendance table. In _form.php of attendance I used
<?= $form->field($model, 'status')->dropDownList([ 'Present' => 'Present', 'Absent' => 'Absent', 'Leave' => 'Leave',], ['prompt' => 'Select status']) ?>
for dropdown. Now I want to a dropdown in gridview search columns with property of filtering and searching. I would like my gridview to be filtered by the dropdown list I have. So when I choose a value from the dropdown list, it should search on base of choosed value. Any help would be highly appriciated.
Upvotes: 3
Views: 5392
Reputation: 21
fetch data from tables automatically:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'deptname',
'value' => 'deptname',
'filter'=>ArrayHelper::map(Attendance::find()->joinWith('staff')->orderBy('id')->all(), 'status', 'status'),
],
]
]);
?>
Upvotes: 0
Reputation: 3818
Try this for add drop down in filter.
[
'attribute' => 'name_of_field',
'value' => function($model){
return $model->relationName->name;
},
'filter' => \yii\helpers\ArrayHelper::map(Model::find()->all(), 'id', 'name'),
],
Upvotes: 1
Reputation: 133380
I think your question is about status field
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
........
[
'attribute' => 'status',
'label' => 'Status',
'filter' => [ 'Present' => 'Present', 'Absent' => 'Absent', 'Leave' => 'Leave',]
],
......
Upvotes: 4
Reputation: 303
add this code in your cgridview,
array(
'name'=>'name_of_field',
'value'=>function($data){
echo $data->relation_name->name;
},
'filter'=>CHtml::listData(Model::model()->findAll('condition_if_any'),'id','name'),
'htmlOptions' => array('style' => "text-align:center;"),
),
Upvotes: 1