dongxiongshi9952 2015-03-20 15:13
浏览 143

yII2:日期过滤器如何在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

[
    '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

  • 写回答

1条回答 默认 最新

  • duanguoyin7008 2015-12-30 12:49
    关注

    You can use kartik Date Range Picker for that :

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

    Implementation

    1.In your view : use kartik\daterange\DateRangePicker;

    2.If you are using grid view

    • 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:d-M-Y");
         }
      ]
      

    3.Pick the date in your Controller parse date like

        $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 .

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示