Menus

Friday 5 May 2017

Yii2 gridview widget filter

Yii2 dropdown in gridview widget filter

How to add dropdown list in the filter field Grid View ?


Add this in Gridview columns array:
[
    'attribute' => 'attribute_name',
    'value' => 'attribute_value',
    'filter' => Html::activeDropDownList($searchModel, 'attribute_name', ArrayHelper::map(ModelName::find()->asArray()->all(), 'ID', 'Name'),['class'=>'form-control','prompt' => 'Select Category']),
],




View code as

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\modules\employee\models\Employee;
use yii\helpers\ArrayHelper;


$this->title = 'Employee Log Books';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="employeelogbook-index box box-body table-responsive">
 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Add Employee Log', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            [
                'attribute'=>'employee_staff_id',
                'value'=>'employeeStaff.staff_name',
                'filter' => Html::activeDropDownList($searchModel, 'employee_staff_id', ArrayHelper::map(Employee::find()->where(['status'=>'Working'])->orderBy('staff_name')->all(),'staff_id','staff_name'),['class'=>'form-control','prompt' => 'Select Category']),
                'label'=>'Staff Name',
                'options' => ['width' => '200']
                
            ],
            [
       'attribute' => 'date',
       'format' =>  ['date', 'php:d-m-Y'],    
                'options' => ['width' => '150']
                //'filter'=>false,
   ],          
            //'center_ccid',
            'batch',
            'subject',
            'topic',
            'description',
            'rate',
            'ta',
            'hour',

         
            [        
                'class' => 'yii\grid\ActionColumn',
                'contentOptions' => ['style' => 'width:70px;'],
                'header'=>'Act',
                'template' => '{view} {update} {delete}',
                'options' => ['width' => '70']
            ],
        ],
    ]); ?>
</div>


Other solutions
[
    'attribute'=>'attribute name',
    'filter'=>array("ID1"=>"Name1","ID2"=>"Name2"),
],
OR
[
    'attribute'=>'attribute name',
    'filter'=>ArrayHelper::map(Model::find()->asArray()->all(), 'ID', 'Name'),
],


DatePicker in the Filter field GridView

use dosamigos\datepicker\DatePicker;


add this code in your gridview

        [
       'attribute' => 'date',
                'value' => 'date',
       'format' =>  ['date', 'php:d-m-Y'],    
                'options' => ['width' => '150'],
                'filter'=>DatePicker::widget([
                    'model' => $searchModel,
                    'attribute' => 'date',  
                        'clientOptions' => [
                            'autoclose' => true,
                            'format' => 'yyyy-m-d'
                        ]
                ])
   ],





Yii2: filtering gridview from a form outside the header


<?php echo  $this->render('_search', ['model' => $searchModel]) ?>

 <?php 
     echo GridView::widget([
         'dataProvider' => $dataProvider,
         'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
             ......
        ]);

 ?>

http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#separate-filter-form





How to add a date range picker to filter for dates on a GridView for Yii2