简体   繁体   English

yII2:日期过滤器如何在Gridview中过滤

[英]yII2:Date filter how to in Gridview

Well I can filter on any fields including the column from related fields, but I wonder how I can filter on date fields. 好吧,我可以过滤任何字段,包括相关字段中的列,但我想知道如何过滤日期字段。

One solution I came across is date-picker for filter, I have not tested this, but my requirement is little different. 我遇到的一个解决方案是过滤器的日期选择器,我没有测试过这个,但我的要求没什么不同。

for example I have duplicated the datetime column in gridview and formatted it like 例如,我在gridview中复制了datetime列并将其格式化为

[
    'attribute'=>'discharge_date',
    'format'=>['DateTime','php:M']
],

So that the column will show only months.The column is showing the months correctly. 因此该列仅显示几个月。该列正确显示月份。

Now I want to filter by month on this column. 现在我想按月过滤此列。 Any suggestion will be greatly appreciated. 任何建议将不胜感激。 Thanks. 谢谢。

I did try like this 我确实这样试过

[
     'attribute'=>'discharge_date',
     'value'=>'discharge_date',
     'filter' => ['2015-01'=>'Jan','2015-02'=>'Feb','2015-03'=>'Mar'],
     'format'=>['DateTime','php:M']

  ],

This works fine, but here the year is being hard-coded, which I don't want. 这工作正常,但今年是硬编码,我不想要。 I know this is not the proper way. 我知道这不是正确的方法。 Thanks 谢谢

You can use kartik Date Range Picker for that : 您可以使用kartik日期范围选择器:

1.check this out : https://github.com/kartik-v/yii2-date-range 1.检查出来: https//github.com/kartik-v/yii2-date-range

Implementation 履行

1.In your view : use kartik\\daterange\\DateRangePicker; 1.在您的视图中:使用kartik \\ daterange \\ DateRangePicker;

2.If you are using grid view 2.如果您正在使用网格视图

  • Add this code in attribute 在属性中添加此代码

     [ 'attribute'=>'Date', 'label'=>'Date', 'format'=>'text', 'filter'=> '<div class="drp-container input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>'. DateRangePicker::widget([ 'name' => 'ItemOrderSearch[created_at]', 'pluginOptions'=>[ 'locale'=>[ 'separator'=>'to', ], 'opens'=>'right' ] ]) . '</div>', 'content'=>function($data){ return Yii::$app->formatter->asDatetime($data['created_at'], "php:dMY"); } ] 

3.Pick the date in your Controller parse date like 3.在Controller分析日期中选择日期,如:

    $date = explode( 'to', $item['date']) // Temporary store into variable as an array
    // $date[0]  =>  12/07/2015 - from date
    // $date[1]  =>  12/10/2015 - to date

4.Pass it to your Model and execute query . 4.将其传递给您的模型并执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM