Yii2:使用Kartik日期范围选择器在gridview中进行过滤 [英] Yii2:using kartik date range picker for filtering in gridview
问题描述
我正在尝试使用Kartik日期范围选择器在gridview中进行过滤.
I am trying to use the Kartik Date Range picker for filter in gridview.
我有一个列date_time字段为discharge_date
,该小部件在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.
这是我在Gridview中的代码:
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'
]
])
],
我要去哪里错了?
推荐答案
您需要解析Search模型中的discharge_date过滤器值.例如,以下代码将使用过滤器值并将其在破折号上分割,然后将$ start_date和$ end_date添加到$ query字符串中(即,将其添加到 if(!$ this-> validate()){ ):
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;
}
这篇关于Yii2:使用Kartik日期范围选择器在gridview中进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!