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 .

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法