Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
4133
Incorrect value in igDateEditor, valueChanged event firing when value is unchanged
posted

I was working with an igDateEditor() and was getting valueChanged() events firing at incorrect times, such as when a user focused and blurred the date editor control, but never changed the value.  I found out that there was a difference in value when getting the igDateEditor("value") at different times.

If you set the value of igDateEditor to a Date object, then query the value, you get the original Date object value you set it to, but if you focus and blur the igDateEditor then query the value, you get a value that is then formatted according to the igDateEditor option "dateInputFormat".  If dateInputFormat is "date", the value of igDateEditor("value") is stripped of the time, even when the user never changed the value, but simply focused and unfocused the control.

I have included a code sample below:

    function log(message) {
        alert(message);
    }
    $(function () {
 
        var dp = $("#datePicker");
        var initDate = new Date(1423779102730-0500); //2015-02-12 17:11:42
    
        dp.igDateEditor({
            dataMode: 'date',
            dateDisplayFormat: 'date',
            dateInputFormat: 'date'
        });
    
        // if control is untouched by the user, we get the full-formatted, original value back
        dp.igDateEditor("value", initDate);
        log(dp.igDateEditor("value"));
    
        // until the user focuses and unfocuses the control, then the
        // value is formatted depending on the dateInputFormat
        dp.focus();
        dp.blur();
        log(dp.igDateEditor("value"));
 
    });

This gives us two different values, though the value was never manually changed:

Thu Feb 12 2015 17:11:42 GMT-0500 (Eastern Standard Time)
Thu Feb 12 2015 00:00:00 GMT-0500 (Eastern Standard Time)

Can you verify this is the intended functionality?  This scenario is kind of odd, and I've had to essentially wrap some of the Ignite UI functions and implement value formatting myself to avoid the valueChanged() event.

Thanks.

Parents
  • 10685
    Verified Answer
    Offline posted

    Hello rehemann, 

    The valueChanged can be raised on lost focus or on spin events. The expected behavior is this event to fire only when there is an actual value change. For example if you enter a date, blur, focus - enter editing and reenter the same date, valueChanged should not be fired (Or anywhere in between). 

    However, in this particular case you are experiencing it is to be considered expected. The reason for valueChanged firing is that there is a change happening in the editor’s point view.

    This is because dateInputFormat is set to date and not dateTime. Using date, when entering new date it is expected there will not be any time fragment inputted by the user and it is automatically set as 00:00:00. In other words, when focusing the editor, the old time fragment is the one set at the initialization, and the new one is automatically set to 00:00:00. It could of course not be obvious for the user as dateInputFormat is set to date.

    If dateTime input format is used instead, the editor will not make any changes to the time fragment and persist the initial one, even the changes made are to the date portion only. If it an appropriate option for you using DateTime format, I suggest just using this approach. 

    In case the above is not an option, in order to persist the time portion, I suggest doing it manually. For example handling valueChanging event and canceling the event if there is no change to the date fragment.

    Another approach could be to implement manual persistence for the time. 

    For reference:
    dataMode- Note: that "date" is used as default.
    dateDisplayFormat- If value is not set, then the dateInputFormat is used automatically
    dateInputFormat - "date": the datePattern member of regional option is used 

    Please let me know how my suggestions work for you.

Reply Children
No Data