敲门自定义绑定与bootstrap datepicker显示错误的视图

时间:2013-02-25 15:40:10

标签: twitter-bootstrap knockout.js

我想显示一个日期选择器,仅显示年份和月份,当前月份为默认视图。问题是它仍然显示日视图,我看不出有什么问题,有什么建议吗?

ko.bindingHandlers.datePickerMonthYear =
    init: (element, valueAccessor, allBindingsAccessor, viewModel) ->
        value = valueAccessor()
        $('#dpMonths').data({date: value()});
        $('#dpMonths').datepicker('update');

        $(element).datepicker(
            viewMode: "years", 
            minViewMode: "months"
        ).on('changeDate', (ev) ->  
            $(this).datepicker('hide')
        ).on('click',  (ev) ->
            $(this).datepicker('show')
        )
        ko.bindingHandlers.validationCore.init(element, valueAccessor,allBindingsAccessor)
    ,
    update: (element, valueAccessor, allBindingsAccessor, viewModel) ->

HTML

<div id="dpMonths" class="input-append date" data-date-minviewmode="months" data-date-viewmode="years" data-date-format="mm/yyyy" data-bind="datePickerMonthYear: startDate">
    <input class="span2" type="text" readonly="" data-bind="value: startDate">
    <span class="add-on">
        <i class="icon-calendar"></i>
    </span>
</div>

2 个答案:

答案 0 :(得分:0)

根据this,您需要设置startView属性:

$(element).datepicker({
    startView: year
})

答案 1 :(得分:0)

尝试在your fiddle中使用dateFormat代替format,如下所示:

$(document).ready(function() {
    $('#input_date').datepicker({
        dateFormat: 'mm/yy',
        viewMode: "months", 
        minViewMode: "years",
        startView: "month"
    });
});

在这里工作:http://jsfiddle.net/jham/ESX6Z/1/