简体   繁体   English

如何将jquery-ui datepicker本地化选项转换为coffee脚本?

[英]How to translate jquery-ui datepicker localization options into coffee script?

I am new to both javascript and coffeescript . 我是javascriptcoffeescript I am trying to initialize my datepicker to another language. 我正在尝试将日期选择器初始化为另一种语言。

Given the following snippet in the documentation ... 给定文档中的以下代码段...

$(function() {
    $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );
});

If I do the following in my .js.coffee file in a Rails 4 app, it doesn't seem to be working. 如果我在Rails 4应用程序的.js.coffee文件中执行以下操作,则它似乎无法正常工作。

jQuery ->
    $('#order_expected_delivery_date').datepicker
        dateFormat: 'yy-mm-dd'
        showAnim: 'slideDown'
        minDate: '+3D'
        maxDate: '+1M +3D'
        $.datepicker.regional[ "zh-CN" ] 

Many thanks in advance. 提前谢谢了。

If you look at $.datapicker.regional['zh-CN'] , you'll see something like this: 如果您查看$.datapicker.regional['zh-CN'] ,则会看到类似以下内容:

{
    closeText: ...,
    prevText: ...,
    ...
}

so the values in $.datepicker.regional are objects which hold a set of localized options for the datepicker. 因此$.datepicker.regional中的值是保存日期选择器的一组本地化选项的对象。 That means that your datepicker call really looks like this: 这意味着您的datepicker调用实际上看起来像这样:

$('#order_expected_delivery_date').datepicker
    dateFormat: 'yy-mm-dd'
    showAnim: 'slideDown'
    minDate: '+3D'
    maxDate: '+1M +3D'
    { closeText: '...', prevText: '...', ... }

and that is interpreted as two arguments to datepicker : 这被解释为datepicker 两个参数:

$('#order_expected_delivery_date').datepicker({
  dateFormat: 'yy-mm-dd', ...
}, {
  closeText: '...', ...
})

and the localization options are ignored. 并且本地化选项将被忽略。

Probably the easiest thing to do is to merge the localization options into your options. 可能最简单的方法是将本地化选项合并到您的选项中。

    options = $.extend({ }, $.datapicker.regional['zh-CN'], 
        dateFormat: 'yy-mm-dd'
        showAnim: 'slideDown'
        minDate: '+3D'
        maxDate: '+1M +3D'
    )
    $('#order_expected_delivery_date').datepicker options

I use $.extend since you're already using jQuery. 我使用$.extend因为您已经在使用jQuery。 I also put the standard localized options before your options since dateFormat might be provided by the localized options and you want to use your own. 我还将标准的本地化选项放在您的选项之前,因为dateFormat可能由本地化选项提供,并且您想使用自己的选项。

You could also use the option method like the example does: 您还可以像示例一样使用option方法

$('#order_expected_delivery_date').datepicker 'option', $.datepicker.regional['zh-CN']

but that might overwrite your dateFormat so some care might be needed. 但这可能会覆盖您的dateFormat因此可能需要小心。

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

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