Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2:using kartik date range picker for filtering in gridview

I am trying to use the Kartik Date Range picker for filter in gridview.

I have a column date_time field as discharge_date, the widget is showing fine in the gridview but the filtering is not working at all.

This is my code in the Gridview:

[
                'attribute'=>'discharge_date',
                'value'=>'discharge_date',                
                'filterType' => GridView::FILTER_DATE_RANGE,
                'filterWidgetOptions' =>([
                'model'=>$model,
                'attribute'=>'discharge_date',
                'presetDropdown'=>TRUE,                
                'convertFormat'=>true,                
                'pluginOptions'=>[                                          
                    'format'=>'Y-m-d',
                    'opens'=>'left'
                ]
            ])

            ],

Where I am going wrong?

Discharge Date

like image 497
Pawan Avatar asked Dec 24 '22 21:12

Pawan


1 Answers

You need to parse the discharge_date filter value in your Search model. As an example the following code will take the filter value and split it on the dash and add the $start_date and $end_date to the $query string (i.e. add it after if (!$this->validate()) {):

if ( ! is_null($this->discharge_date) && strpos($this->discharge_date, ' - ') !== false ) {
    list($start_date, $end_date) = explode(' - ', $this->discharge_date);
    $query->andFilterWhere(['between', 'data_date', $start_date, $end_date]);
    $this->discharge_date = null;
}
like image 121
AFS Avatar answered Dec 28 '22 09:12

AFS