yii \ jui \ DatePicker是否支持朱利安日期?

时间:2015-03-06 13:00:37

标签: datepicker yii2 julian-date

我发现DatePicker中的默认设置不会考虑Julian日期(即1582年之前的欧洲日期)​​,但还没有找到解决此问题的正确方法。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我可能会为jQuery实现Keith Woods Calendar。作为默认日期选择器进行编码可能不太友好,但将其分配给$form->textInput()应该很简单。

答案 1 :(得分:0)

所以到目前为止,DataPicker无法考虑除Gregorian之外的任何其他日历。只是为了记录,我使用Keith Wood的世界日历日期选择器发布我的解决方案:

1)创建web/calendars/并解压缩日历包。

2)创建资产包CalendarAsset.phpassets文件夹(我选择了Julian / Gregorian(默认)日历和英语/西班牙语本地化;您可能想要从其他语言和日历中选择):

<?php

namespace app\assets;

use yii\web\AssetBundle;

class CalendarAsset extends AssetBundle 
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'calendars/humanity.calendars.picker.css',
    ];
    public $js = [
        'calendars/jquery.min.js',
        'calendars/jquery.calendars.js',
        'calendars/jquery.calendars.plus.js',
        'calendars/jquery.plugin.js',
        'calendars/jquery.calendars.picker.js',
        'calendars/jquery.calendars.julian.js',
        'calendars/jquery.calendars.picker-es.js',
        'calendars/jquery.calendars-es.js',
     ];
     public $jsOptions = [
      'position' => \yii\web\View::POS_HEAD,
     ];
     public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
     ];
}

?>

3)然后我添加了web/calendars/jquery.mycalendar.calendars.js以包含一些默认设置,一个日历选择器以及完全从Keith Wood演示中删除的常用实现:

$.calendarsPicker.setDefaults({
         dateFormat: 'yyyy-mm-dd',
         defaultDate: '1478-06-24',
         yearRange: 'c-120:c+120',
        });

$('#selectCalendar').change(function() { 
    var calendar = $.calendars.instance($(this).val(), 'es'); 
    var convert = function(value) { 
    return (!value || typeof value != 'object' ? value : 
        calendar.fromJD(value.toJD())); 
};

$('.is-calendarsPicker').each(function() { 
    var current = $(this).calendarsPicker('option');
    $(this).calendarsPicker('option', {calendar: calendar,
            onSelect: null, onChangeMonthYear: null, 
            defaultDate: convert(current.defaultDate),
            minDate: convert(current.minDate), 
            maxDate: convert(current.maxDate),
            }). 
            calendarsPicker('option', {
                  onSelect: current.onSelect, 
                  onChangeMonthYear: current.onChangeMonthYear
                  });
  }); 
});

4)最后我把它全部带入了_form.php视图:

<?php

use app\assets\CalendarAsset;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

// Load Keith Wood's JQuery stuff selected in CalendarAsset.php.

CalendarAsset::register($this);

// Load my calendar defaults and implementation.

$this->registerJsFile('calendars/jquery.mycalendar.calendars.js');

// Link one calendar instantiation to id = "popupDatepicker" with Julian and spanish as defaults.

$this->registerJs("$('#popupDatepicker').calendarsPicker({calendar: $.calendars.instance('julian', 'es')});");

?>

...........

<?php $form = ActiveForm::begin(); ?>

<?= Html::label(Yii::t('app', 'Calendario'), 'selectCalendar', ['class' => 'control-label']) ?>

<div class="form-group">

<?= Html::dropDownList(null, null, [
          'julian' => Yii::t('app', 'Calendario juliano'),
          'gregorian' => Yii::t('app', 'Calendario Gregoriano'),
], ['id' => 'selectCalendar', 'class' => 'form-control']) ?>

</div>

<?= $form->field($model, 'fecha')->textInput(['id' => 'popupDatepicker', 'placeholder' => Yii::t('app', 'aaaa-mm-dd')]); ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Crear') : Yii::t('app', 'Actualizar'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

希望它对某人有用。