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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With