简体   繁体   English

如果在GXT中某些条件失败,如何将DateField值设置为Previous Value

[英]How to set the DateField value to the Previous Value if certain condition fails in GXT

I am creating a DateField and adding listener to it.If certain conditions fails I need to reset the value of DateField with previous value which is there in that field.Below is my code 我正在创建一个DateField并添加侦听器。如果某些条件失败,则需要使用该字段中的先前值重置DateField的值。以下是我的代码

final DateField dateField1 = new DateField();
    dateField1.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MMM/yyyy")); 
    dateField1.getDatePicker().addListener(Events.Select, new Listener<DatePickerEvent>() {

        @Override
        public void handleEvent(DatePickerEvent dpe) {

        //  Window.alert("Getting Roster Date here-->"+grid.getColumnModel().);
        Window.alert("Getting RosterDate-->"+   caseStoreModule.getModifiedRecords().get(0).get("rosterDate"));
         if(caseStoreModule.getModifiedRecords().get(0).get("rosterDate")!=null){
                DateTimeFormat format = DateTimeFormat.getFormat("dd/MMM/yyyy");
                rosterdate=format.parse(caseStoreModule.getModifiedRecords().get(0).get("rosterDate").toString());
                nextdate.setTime(rosterdate.getTime()+(1000*60*60*24));
                prevdate.setTime(rosterdate.getTime()-(1000*60*60*24));
            }
        checkInDate=(Date)(dateField1.getValue());
        if(checkInDate.getTime()<rosterdate.getTime() || checkInDate.getTime()>nextdate.getTime()){
            MsgBox.info("Enter valid Check In Date");
           dateField1.setValue();//here i need to reset the value to the     previous value.
            return ;
        }



        }
    });

If the if condition is true,then I need to put the previous value which is there in the Field instead of reseting it.Please suggest how to do this. 如果if条件为true,那么我需要在字段中放入先前的值,而不是将其重置。请建议如何执行此操作。

One solution is having a String variable that holds the field value before doing any manipulation. 一种解决方案是让String变量在执行任何操作之前保存字段值。 So if you need to reset the field just use it. 因此,如果您需要重置该字段,请使用它。 Something like this 像这样

String tmpStringValue = dateField1.getValue();
...
if(something went wrong){
   dateField1.setValue(tmpStringValue );
}

You can store values as an attribute of Element . 您可以将值存储为Element的属性。

At first time this attribute oldValue will be blank. 第一次,该属性oldValue将为空白。 Set this attribute every time if validation is successful. 如果验证成功,则每次设置此属性。

For more information read inline comments. 有关更多信息,请阅读内联注释。

Here is the sample code: 这是示例代码:

dateField1.getCell().getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {

    @Override
    public void onValueChange(ValueChangeEvent<Date> event) {

        DateTimeFormat format = DateTimeFormat.getFormat("yyyy-dd-MM");
        String oldValue = dateField1.getElement().getAttribute("oldValue");

        if (oldValue == null || oldValue.length() == 0) {
            // initially date field is empty
            dateField1.getElement().setAttribute("oldValue",
                    format.format(event.getValue()));
        } else {
            Date oldDate = format.parse(oldValue);
            Date newDate = event.getValue();

            // you validation logic
            if (oldDate.getTime() < newDate.getTime()) {
                // revert back to last value if validation is failed
                dateField1.getCell().getDatePicker().setValue(oldDate);
            } else {
                // set the old value with new one 
                dateField1.getElement().setAttribute("oldValue",
                        format.format(event.getValue()));
            }
        }

    }
});

Call below line if value is initially populated by its default value just after setting the value. 如果在设置值后立即使用默认值填充值,则在行下方调用。

dateField1.getElement().setAttribute("oldValue",
                    format.format(event.getValue()));

In GXT, every Field has a setOriginalValue(D originalValue) method which could help you in this matter too. 在GXT中,每个Field都有一个setOriginalValue(D originalValue)方法,该方法也可以帮助您。 You can always return to the original value by reset() ing the Field. 您始终可以通过reset() Field来返回到原始值。 Please keep in mind that reset() will clear the validation messages too! 请记住, reset()也会清除验证消息!

How to use it in situations like this? 在这种情况下如何使用它?

  1. Set the original value when the entered value is validated fine 确认输入值正确后,设置原始值
  2. Call Field.reset() , if anything went wrong to reload the last working value in your field. 如果发生任何错误,请致电Field.reset() ,以重新加载字段中的最后一个工作值。

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

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