3

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.

YakovL
  • 5,213
  • 10
  • 46
  • 71
raxa
  • 481
  • 7
  • 17

4 Answers4

4

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',]
        ],
        ......
scaisEdge
  • 124,973
  • 10
  • 73
  • 87
1

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;"),
    ),
Adi
  • 163
  • 1
  • 9
1

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'),
],
GAMITG
  • 3,670
  • 7
  • 30
  • 50
  • "+1" thanx for your help .this code also run for me with the changes 'filter' => \yii\helpers\ArrayHelper::map(Model::find()->all(), 'status', 'status'), – raxa Aug 26 '15 at 06:18
0

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'),
            ],
        ]
    ]);
?>
YakovL
  • 5,213
  • 10
  • 46
  • 71