Menus

Tuesday 26 September 2017

Yii2 DatePicker



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




The ModelSearch class
Your model Students has a date_of_join attribute and we want to add a date range filtering on that value.

declare variable
    public $date_from;
    public $date_to;
add in search function

if($this->date_from && $this->date_to) {
$query->andFilterWhere(['between', 'date_of_join', date('Y-m-d',strtotime($this->date_from)),  date('Y-m-d',strtotime($this->date_to))]);
}


Configuring GridView column

use widget
use dosamigos\datepicker\DateRangePicker;

filtering column

         
                'format'=>'raw',                
                'filter'=>  DateRangePicker::widget([
                                                        'model' => $searchModel,
                                                        'attribute' => 'date_from',
                                                        'attributeTo' => 'date_to',
                                                       'clientOptions' => [
                                                            'autoclose' => true,
                                                            'format' => 'd-m-yyyy',  
                                                            'todayBtn' => true,
                                                        ]
                                                    ]),                                              
                'attribute' => 'date_of_join',           
       'format' =>  ['date', 'php:d-m-Y'],
       'options' => ['width' => '1000'],                
   ],   

Related site - http://www.2amigos.us/blog/how-to-add-a-date-range-picker-to-filter-for-dates-on-a-gridview-for-yii2

Yii2 date range filter

'columns' => [     [          'attribute' => 'date',          'filter' => DateRangePicker::widget(...),     ] ] 



Related Links

Yii2 gridview widget filter

No comments:

Post a Comment