Extjs связывают fromDate и ToDate в календаре выбора даты - PullRequest
0 голосов
/ 22 ноября 2018

Я хочу 2 условия, использующих связывание:

  1. toDate не должен быть меньше, чем fromdate
  2. на сегодняшний день не должно быть больше, чем (fromdate + 1 год), т.е. maxvalue = fromdate + 1год

Это код:

        var me = this;
        return [ {
            xtype : 'form',
            layout : 'hbox',
            border : false,
            items : [ {
                xtype : 'datefield',
                name : 'fromDate',
//              allowBlank : false,
                width : 110,
                fieldLabel : 'From',
                labelAlign : 'top',
                emptyText : 'mm/dd/yyyy',
                submitEmptyText : false,
                invalidText : '{0} is not a valid date!',
                fieldStyle : "font: bold 12px sans-serif;background:#fff;color:#444;",
                padding : 5,
                validator: function(value) {
                    if(value){
                        if(new Date(value) > new Date){
                            return 'From date must be less than current date.';
                        }else
                            return true;
                    } else {
                        return 'From date is required.';
                    }
                    var isRequired = me.oneFieldRequired();
                    if(typeof isRequired === "string")
                        return isRequired;
                    return me.validateName(value);
                }
            }, {
                xtype : 'datefield',
                name : 'toDate',
                width : 110,
                fieldLabel : 'To',
                labelAlign : 'top',
                emptyText : 'mm/dd/yyyy',
                invalidText : '{0} is not a valid date!',
                fieldStyle : "font: bold 12px sans-serif;background:#fff;color:#444;",
                padding : 5
            }, {
                xtype : 'button',
                padding : '4 8',
                margin : '30 0 0 3',
                text : 'Go',
                handler : function(btn) {
                    var thisForm = btn.up('form').getForm();
                    var formValues = thisForm.getValues();
                    if(!thisForm.findField("fromDate").validate())
                        return;

                    var toDate = formValues.toDate ? new Date(formValues.toDate) : new Date(),
                        t = toDate.getTime() - new Date(formValues.fromDate).getTime(),
                        cm = 24 * 60 * 60 * 1000 * 30,
                        monthRange = Math.floor(t / cm);
                    if(monthRange >= 0){
                        var ToDate = toDate;
                        if(monthRange > 11){
                            var fd = new Date(formValues.fromDate);
                            fd.setMonth(fd.getMonth()+11);
                            ToDate = new Date(fd.getFullYear(),fd.getMonth() + 1, 0);
                        }
                        ToDate = (ToDate.getMonth() + 1) + "/" + ToDate.getDate() + "/" + ToDate.getFullYear();
                        Ext.StoreManager.lookup('Visits').eAjax({
                            empiRefId : PatientManager.getPatientId(),
                            fromDate : formValues.fromDate,
                            toDate : ToDate,
                            preference : {
                                enableVisitTypeFilter : true,
                                outputType : 'highLevel'
                            }
                        }, this, {
                            success : function(records) {
                                me.showCustomView(records, formValues.fromDate, ToDate);
                            },
                            failure : function(response) {},
                            empty : function(response) {
                                me.showCustomView([], formValues.fromDate, ToDate);
                            }

                        });
                        btn.up('tooltip').close();
                    }
                }
            } ]
        } ];
...