Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DateTimeItem does not work as expected

    I am trying to create a form that contains a field where I can select a date and a time. This is in general possible by creating a DynamicForm and placing a DateTimeItem in it. This is working so far. However when I want to change the time portion of the fields value, the change is not reflected to the backing JS object. I created the following short snippet to demonstrate the behaviour:

    Code:
    final DynamicForm dynamicForm = new DynamicForm();
    final DateTimeItem dateTimeItem = new DateTimeItem();
    final ButtonItem buttonItem = new ButtonItem();
    
    dateTimeItem.setTitle("My clean item");
    buttonItem.setTitle("Log value");
    buttonItem.addClickHandler(e -> GWT.log(dateTimeItem.getValue().toString()));
    dynamicForm.setItems(dateTimeItem, buttonItem);
    Now do the following:
    1. Select a date and a time (e.g. 2018-02-21 09:00)
    2. Press the button (Logs 2018-02-21 09:00)
    3. Change the time portion either using the chooser or by manipulating the text field (e.g. 2018-02-21 10:00)
    4. Press the button (Logs 2018-02-21 09:00 and not 2018-02-21 10:00)
    I am using the following SmartGWT version and browsers:

    SmartGWT version: v11.1p_2018-01-23/LGPL Development Only
    Browser: Chrome 64, Firefox 58
    Last edited by hanswurst2; 22 Feb 2018, 00:00.

    #2
    Please be sure to wrap code samples in CODE tags so we can read them properly.

    The problem here is that you are expecting dateTimeItem to be a reference to the live FormItem you can see onscreen - but it isn't, it's a set of properties used to create the live FormItem you can see.

    So, you just want to call form.getValue(...) or form.getItem(...).getValue() instead.

    Comment


      #3
      This does not make any difference unfortunately. I extended the snippet to the following:

      Code:
      final DynamicForm dynamicForm = new DynamicForm();
      final DateTimeItem dateTimeItem = new DateTimeItem("date_time_item");
      final ButtonItem buttonItem = new ButtonItem("button_item");
      
      dateTimeItem.setTitle("My clean item");
      dateTimeItem.addChangedHandler(e -> GWT.log(e.getItem().getValue().toString()));
      buttonItem.setTitle("Log value");
      buttonItem.addClickHandler(e -> GWT.log(getItem("date_time_item").getValue().toString()));
      dynamicForm.setItems(dateTimeItem, buttonItem);
      The changed handler is always fired when I change the date portion, but it is never fired when I just change the time portion.

      So when dateTimeItem is not a reference to the item why am I able to retrieve a value from it? And why does it have the method getValueAsDate if I may not use it?
      Last edited by hanswurst2; 22 Feb 2018, 03:39.

      Comment


        #4
        We're showing your sample working as expected - you may need to update to the latest patched build from smartclient.com/builds, as a fix was made recently in roughly this area.

        Note that DateTimeItems don't fire changed() handlers as you type, because the value would almost always be incomplete - with your sample, we see the "changed" log happen when the dateTimeItem blur()s, either because we tab out of the field, or click the button.

        Also, your button click handler wants to call dynamicForm.getItem(), not just getItem()
        Last edited by Isomorphic; 22 Feb 2018, 01:10.

        Comment


          #5
          Well, I have to say that your suggestions were not helpful for me. I ended up with extending the DateTimeItem. Although this is even more hacky than SmartGwt, it is working for me:

          Code:
          import java.util.Date;
          
          import com.google.gwt.i18n.client.DateTimeFormat;
          import com.smartgwt.client.widgets.form.fields.DateTimeItem;
          
          public class MatureDateTimeItem extends DateTimeItem {
          
              @Override
              public Object getValue() {
                  return getValueAsDate();
              }
          
              @Override
              public Date getValueAsDate() {
                  final String rawValue = getTextField().getValueAsString();
          
                  return DateTimeFormat.getFormat(getFormat()).parse(rawValue);
              }
          
          }
          You have to call setFormat() on your items, otherwise it won't work.

          Please note that this breaks at least change handlers. I am pretty sure that this will break even more.
          Last edited by hanswurst2; 28 Feb 2018, 23:07.

          Comment


            #6
            For any other readers: we don't recommend using this code, as there doesn't appear to be an actual issue with the framework here.

            We'd also recommend that the OP raise the maturity level of his posts, if he expects any further help from Isomorphic or the community.

            Comment


              #7
              Originally posted by hanswurst2 View Post
              Well, I have to say that your suggestions were not helpful for me. I ended up with extending the DateTimeItem. Although this is even more hacky than SmartGwt, it is working for me:

              Code:
              import java.util.Date;
              
              import com.google.gwt.i18n.client.DateTimeFormat;
              import com.smartgwt.client.widgets.form.fields.DateTimeItem;
              
              public class FuckYouSmartGwtDateTimeItem extends DateTimeItem {
              
              @Override
              public Object getValue() {
              return getValueAsDate();
              }
              
              @Override
              public Date getValueAsDate() {
              final String rawValue = getTextField().getValueAsString();
              
              return DateTimeFormat.getFormat(getFormat()).parse(rawValue);
              }
              
              }
              You have to call setFormat() on your items, otherwise it won't work.

              Please note that this breaks at least change handlers. I am pretty sure that this will break even more.
              I have the exact same problem and this code works like a charm.

              Comment


                #8
                Sorry for being rude Isomorphic team :(

                I promise that I'll be mature as soon as SmartGWT becomes mature.

                Comment


                  #9
                  For any other readers: the two posters above, who seem to confirm each other's findings, are just two accounts created by the same person. There is no actual bug here and the user's code should not be used.

                  Hans, despite baselessly insulting our software twice, and childishly creating a second account in an attempt to harm community members, we're going to help you out anyway: what you did wrong is that you are running in a non-US locale, but you haven't set the GWT locale to match. This is covered in our docs: see the Internationalization overview. It's also in the core GWT docs.

                  Now, since you promised above to be mature when SmartGWT is mature, and you've been shown this "issue" you found was due to your own blunder, we expect some maturity from you from here on.

                  Comment

                  Working...
                  X