根据extjs中的日期差异隐藏文本字段

时间:2017-09-22 09:22:26

标签: extjs extjs4 extjs4.2

如何根据extjs日期选择器中选定的日期范围隐藏文本字段?如果开始日期和结束日期之间的差异超过30天,我必须隐藏文本字段。

        {
            title: 'Start Date',
            margin: '0 20 0 0',
            items: {
                xtype: 'datepicker',
                value: Ext.Date.add(new Date(), Ext.Date.DAY, -1),
                itemId: 'startDate',
                showToday: false,
            }
        },
        {
            title: 'End Date',
            margin: '0 20 0 0',
            items: {
                xtype: 'datepicker',
                itemId: 'endDate',
                value: Ext.Date.add(new Date(), Ext.Date.DAY, -1),
                showToday: false
            }
        }
    ]
},
{
    xtype: 'container',
    layout: 'hbox',
    items: [
        {
            margin: '0 20 0 0',
            items: {
                xtype: 'textfield',
                name: 'text'
            }
        }

2 个答案:

答案 0 :(得分:2)

您可以收听datepicker的select事件并获取&找到两个日期之间的差异并相应地隐藏文本字段。

    listeners: {
     select: function(datepicker, startDate) {
     var endDate = datepicker.up('form').down('#endDate').getValue();
     var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
     var diffDays = Math.round(Math.abs((endDate.getTime() - startDate.getTime()) / (oneDay)));
     if (diffDays > 30) {
          datepicker.up('form').down('[name=text]').hide();
         } else {
          datepicker.up('form').down('[name=text]').show();
         }
    }
   }

使用handler

handler: function(datepicker, startDate) {
             var endDate = datepicker.up('form').down('#endDate').getValue();
             var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
             var diffDays = Math.round(Math.abs((endDate.getTime() - startDate.getTime()) / (oneDay)));
             if (diffDays > 30) {
                  datepicker.up('form').down('[name=text]').hide();
                 } else {
                  datepicker.up('form').down('[name=text]').show();
                 }
            }

示例工作代码:



Ext.application({
      name: 'Fiddle',

      launch: function() {
        Ext.create('Ext.form.Panel', {
            title: 'Simple Form',
            // The fields
            items: [{
                title: 'Start Date',
                margin: '0 20 0 0',
                items: {
                  xtype: 'datepicker',
                  value: Ext.Date.add(new Date(), Ext.Date.DAY, -1),
                  itemId: 'startDate',
                  showToday: false,
                  handler: function(datepicker, startDate) {

                       var endDate = datepicker.up('form').down('#endDate').getValue();
                       var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
                       var diffDays = Math.round(Math.abs((endDate.getTime() - startDate.getTime()) / (oneDay)));
                       if (diffDays > 30) {
                         datepicker.up('form').down('[name=text]').hide();
                       } else {
                         datepicker.up('form').down('[name=text]').show();
                       }
                     }
                   
                }
                }, {

                title: 'End Date',
                margin: '0 20 0 0',
                items: {
                  xtype: 'datepicker',
                  itemId: 'endDate',
                  value: Ext.Date.add(new Date(), Ext.Date.DAY, -1),
                  showToday: false,
                  handler: function(datepicker, endDate) {

                       var startDate = datepicker.up('form').down('#startDate').getValue();
                       var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
                       var diffDays = Math.round(Math.abs((endDate.getTime() - startDate.getTime()) / (oneDay)));
                       if (diffDays > 30) {
                         datepicker.up('form').down('[name=text]').hide();
                       } else {
                         datepicker.up('form').down('[name=text]').show();
                       }
                     }
                   
                },
                },
                {
                  xtype: 'container',
                  layout: 'hbox',
                  items: [{
                    margin: '0 20 0 0',
                    items: {
                      xtype: 'textfield',
                      name: 'text'

                    }
                  }]
                }],
              renderTo: Ext.getBody()
            });
        }
      });

<link rel="stylesheet" href="https://cdn.sencha.com/ext/gpl/4.1.1/resources/css/ext-all.css">
<script type="text/javascript"  src="https://cdn.sencha.com/ext/gpl/4.1.1/ext-all-debug.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

  

在ExtJs中,Date singletone class这里有计算差异bw 2个日期的方法

我创建了一个小型演示。您可以看到它的工作原理Sencha Fiddle

希望它能帮助您解决问题。

Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    width: 300,
    bodyPadding: 10,
    title: 'Dates',
    items: [{
        xtype: 'datefield',
        anchor: '100%',
        fieldLabel: 'Start Date',
        name: 'from_date',
        listeners: {
            select: function () {
                this.up().onDateSelect();
            }
        }
    }, {
        xtype: 'datefield',
        anchor: '100%',
        fieldLabel: 'End Date',
        name: 'to_date',
        listeners: {
            select: function () {
                this.up().onDateSelect();
            }
        }
    }, {
        xtype: 'textfield',
        fieldLabel: 'Hidable field',
        name: 'fName'
    }],
    onDateSelect: function () {
        var startDate = this.down('[name=from_date]').getValue(),
            endDate = this.down('[name=to_date]').getValue(),
            textField = this.down('[name=fName]');
        textField.setHidden(Ext.Date.diff(startDate, endDate, 'd') > 30);
    }
});