drjltlm156790 2017-06-28 08:46
浏览 417
已采纳

具有两个属性的Kartik DateRangePicker会在范围日期选择中导致invalidDateFormat和NaN

I've a question about the Kartik DateRangePicker Widget that i use in my gridview for filter some results.

in my SearchModel i've created two attributes

    public $date_start;
    public $date_end;

i use these for filter a field in database 'insert_date'.

In my view, as grid configuration, i've set these options

[
            'attribute' => 'insert_date',
            'options' => ['class' => 'gridview-date-column'],
            'filter' => DateRangePicker::widget([
                'model' => $searchModel,
                'name' => 'insert_date',
                'attribute' => 'insert_date',
                'startAttribute' => 'date_start',
                'endAttribute' => 'date_end',
                'convertFormat'=>true,
                'pluginOptions' => [
                    'opens'=>'right',
                    'locale' => [
                        'cancelLabel' => 'Clear',
                        'format' => 'd-m-Y',
                    ],
                ]
            ]),
            'format' => ['date', Yii::$app->formatter->datetimeFormat],
            'contentOptions'=>['style'=>'min-width: 200px;'] 
        ],

By default $date_start and $date_end haven't a value, so , when i enter in my view and try to filter this field i get an 'invalidDate' error and a series of NaN on the calendars.

This is fixed if i set a value for these two fields or if remove them from the configuration ( so i can only use insert_date attribute as string with these two ranges for filtering ).

Looking in the plugin repository i've found the same case and as response of the author

This problem occurs because you have an invalid date format for the data that does not match the plugin's format.

But as empty these fields can never have a correct data format.

Someone had the same problem? Thanks in advance for all the responses.

  • 写回答

1条回答 默认 最新

  • drbii0359 2017-07-03 09:37
    关注

    I've got the solution:

    In Kartik plugin there's a Behavior Model that i can include in my searchModel:

    use kartik\daterange\DateRangeBehavior;
    

    Then i can instantiate it overriding behaviors() method:

    public $date_start;
    public $date_end;
    
    public function behaviors() {
        return [
            [
                'class' => DateRangeBehavior::className(),
                'attribute' => 'insert_date',
                'dateStartAttribute' => 'date_start',
                'dateEndAttribute' => 'date_end',
                'dateStartFormat' => 'd-m-Y',
                'dateEndFormat' => 'd-m-Y',
    
            ]
        ];
    }
    

    and in the search() method:

    public function search($params)
    {
       // Some other filters
       if($this->date_start && $this->date_end) {
          // filtered query
       }
    }
    

    Hope this can help someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?