Extjs 5 - 仅限日期选择器年份

时间:2016-12-01 11:03:27

标签: javascript extjs datepicker picker

我希望没有日期或月份的日期选择器,只需要一年。

我想要这个

EXTJS 5 - Date picker year and month only

但没有月份,只有一年。

由于

2 个答案:

答案 0 :(得分:3)

您可以覆盖选择器以隐藏月份部分,并在日期字段中将此类用于选择器。我为月份部分制作了“display:none”(以防止更改组件的逻辑)并覆盖了选择器主体的样式(在CSS规则中更好):

    Ext.define('Ext.ux.OnlyYearPicker', {
    xtype: 'onlyyearpicker',
    extend: 'Ext.picker.Month',

    afterRender: function(){
        this.callParent();
        this.el.setStyle({width: '106px',})
    },

    renderTpl: [
    '<div id="{id}-bodyEl" data-ref="bodyEl" class="{baseCls}-body">',
      '<div style="display: none; width:0px;" id="{id}-monthEl" data-ref="monthEl" class="{baseCls}-months">',
          '<tpl for="months">',
              '<div class="{parent.baseCls}-item {parent.baseCls}-month">',
                  '<a style="{parent.monthStyle}" role="button" hidefocus="on" class="{parent.baseCls}-item-inner">{.}</a>',
              '</div>',
          '</tpl>',
      '</div>',
      '<div id="{id}-yearEl" data-ref="yearEl" class="{baseCls}-years">',
          '<div class="{baseCls}-yearnav">',
              '<div class="{baseCls}-yearnav-button-ct">',
                  '<a id="{id}-prevEl" data-ref="prevEl" class="{baseCls}-yearnav-button {baseCls}-yearnav-prev" hidefocus="on" role="button"></a>',
              '</div>',
              '<div class="{baseCls}-yearnav-button-ct">',
                  '<a id="{id}-nextEl" data-ref="nextEl" class="{baseCls}-yearnav-button {baseCls}-yearnav-next" hidefocus="on" role="button"></a>',
              '</div>',
          '</div>',
          '<tpl for="years">',
              '<div class="{parent.baseCls}-item {parent.baseCls}-year">',
                  '<a hidefocus="on" class="{parent.baseCls}-item-inner" role="button">{.}</a>',
              '</div>',
          '</tpl>',
      '</div>',
      '<div class="' + Ext.baseCSSPrefix + 'clear"></div>',
      '<tpl if="showButtons">',
          '<div class="{baseCls}-buttons">{%',
              'var me=values.$comp, okBtn=me.okBtn, cancelBtn=me.cancelBtn;',
              'okBtn.ownerLayout = cancelBtn.ownerLayout = me.componentLayout;',
              'okBtn.ownerCt = cancelBtn.ownerCt = me;',
              'Ext.DomHelper.generateMarkup(okBtn.getRenderTree(), out);',
              'Ext.DomHelper.generateMarkup(cancelBtn.getRenderTree(), out);',
          '%}</div>',
      '</tpl>',
    '</div>'
]
});

你会在fiddler找到更多细节。看起来很丑,但工作。我认为你最好使用像combobox这样的东西。如果DatePicker用户选择了Ok键,那么单个值就很奇怪了。

Fiddler

答案 1 :(得分:0)

对于Chrome有问题的用户,请将createPicker功能转换为:

    createPicker: function () { // Converted function to Chrome
        var me = this,
            format = Ext.String.format,
            pickerConfig;

        pickerConfig = {
            pickerField: me,
            ownerCmp: me,
            renderTo: document.body,
            floating: true,
            hidden: true,
            focusOnShow: true,
            minDate: me.minValue,
            maxDate: me.maxValue,
            disabledDatesRE: me.disabledDatesRE,
            disabledDatesText: me.disabledDatesText,
            disabledDays: me.disabledDays,
            disabledDaysText: me.disabledDaysText,
            format: me.format,
            showToday: me.showToday,
            startDay: me.startDay,
            minText: format(me.minText, me.formatDate(me.minValue)),
            maxText: format(me.maxText, me.formatDate(me.maxValue)),
            listeners: {
                select: {
                    scope: me,
                    fn: me.onSelect
                },
                monthdblclick: {
                    scope: me,
                    fn: me.onOKClick
                },
                yeardblclick: {
                    scope: me,
                    fn: me.onOKClick
                },
                OkClick: {
                    scope: me,
                    fn: me.onOKClick
                },
                CancelClick: {
                    scope: me,
                    fn: me.onCancelClick
                }
            },
            keyNavConfig: {
                esc: function () {
                    me.collapse();
                }
            },
        };

        if (Ext.isChrome) {
            me.originalCollapse = me.collapse;
            pickerConfig.listeners.boxready = {
                fn: function () {
                    this.picker.el.on({
                        mousedown: function () {
                            this.collapse = Ext.emptyFn;
                        },
                        mouseup: function () {
                            this.collapse = this.originalCollapse;
                        },
                        scope: this
                    });
                },
                scope: me,
                single: true
            };
        }

        return Ext.create('Ext.ux.OnlyYearPicker', pickerConfig);
    },

您可以在此处查看示例:https://fiddle.sencha.com/#fiddle/1lmp

如果没有,您的选择器将在每年选择的每次点击时消失。