Announcement

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

    Cell edit parsing/formatting with default value

    I'm trying to use a default value when a user clicks on a button that invokes ListGrid.startEditingNew() and display the value as a mega unit (divided by 1,000,000) but stored as a regular unit. With my code below, when I press the add button, the default value shows up in the editor without formatting. Is there anyway to get to display as formatted? For example, if my default value is 1000000, I'd like the editor to contain 1 when the add button is pressed and then saved as 1000000 after the user finishes editing. Thanks for your help!

    I'm using SmartGWT 2.5p in FF.

    This is my code:
    Code:
    ListGridField field = new ListGridField("field");
    field.setDefaultValue(1000000); // should display as 1
    field.setEditValueParser(new CellEditValueParser() {
        public Object parse(Object value, ListGridRecord record, int rowNum, int colNum) {
            // user enters values as mega unit. Store in regular unit
            return ValueUtils.toFloat(value) * 1000000;
        }
    });
    field.setEditValueFormatter(new CellEditValueFormatter() {
        public Object format(Object value, ListGridRecord record, int rowNum, int colNum) {
            // stored in regular unit. Display as mega unit
            return ValueUtils.toFloat(value) / 1000000;
        }
    });
    
    field.setCellFormatter(new CellFormatter() {
        public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            // for when fetching data from data source
            return ValueUtils.toFloat(value) / 1000000;
        }
    });
    
    ListGrid listGrid = new ListGrid();
    listGrid.setFields(field);

    #2
    setDefaultValue() here is effectively defaulting the value as though the user had typed in 1000000. Try this with a sample dataset consisting of a Record with a field attribute with value 1000000 and you should see it work as expected.

    Comment


      #3
      Code:
      field.setDefaultValue(1);
      I made the above change and it worked.

      I then proceeded to add another field called "id" as the first column. Now when I click the add button, which calls ListGrid.startEditingNew(), if I leave the cursor on the "id" field then click out to finish editing, the display value of the "field" field (the second column) gets set to .0000001. Any ideas?

      The formatting works as expected if I click (or tab over) on the "field" cell.

      Thanks!

      Comment


        #4
        Once again, try creating an actual Record with the value 1000000, or equivalently, calling setEditValue or passing initial values to startEditingNew(). setDefaultValue is essentially populating the field as though the user had typed the given value.

        Comment


          #5
          I figured out that in order for the formatting to work, the user needs to have had focus on that cell. So instead of just calling startEditingNew(), I called startEditing and passed in arguments to focus on the 2nd column. Here is my code if anyone is interested:
          Code:
          // On init
          field.setDefaultValue(1);
          
          ...
          
          // In "add" button click handler...
          listGrid.startEditing(listGrid.getRecords().length, 1, false);

          Comment


            #6
            I've actually ran into this problem again because we have a ListGrid with more than one cell that needs formatting/parsing...

            (sorry I can't bring the code outside but here is a description of what's happening)

            The code is similar to what I posted in the original post but with two ListGridFields that need formatting/parsing. Each have the same set of CellEditParsers/Formatters as before. They each have default values (in non-mega, regular units). Again, I have an add button adds a row containing the default values and has the cursor on the first column so the user can edit it.

            This is what happens...
            1. Press add button (which calls ListGrid.startEditingNew())
            2. Values get displayed on the ListGrid with non-mega units (e.g., 3000000 and 5000000). But I want this to be in mega units (e.g., 3 and 5).
            - The CellFormatter is called here with the input values being in non-mega units. It returns mega units but are not displayed on the ListGrid as such.
            3. When I click out of the ListGrid without editing any values, the first column stays in non-mega (e.g., 3000000) units while the second column is in mega (e.g., 5).

            Behind the scenes on step 3 above:
            Input value -> Parser/Formatter -> Return value
            - 3E6 -> CellEditValueParser -> 3E12
            - 3E12 -> CellEditValueFormatter -> 3E6
            - 3E12 -> CellFormatter -> 3E6
            - 5E6 -> CellFormatter -> 5

            It looks like the CellEditValueParser is not being called on the second column.

            Here are results from other tests I've done with default values, 3E6 and 5E6
            - Tab over to second column, click outside -> 3E6, 5E6
            - Change values to 3 and 5 -> 3, 5

            I have tried calling ListGrid.addData(defaultRecord), which works in that the formatting is correct. But I want the user to be editing when pressing the add button so I tried calling ListGrid.startEditing(listGrid.getTotalNumRecords() - 1, 0, false), but it exhibits similar behavior to what I described above.

            Comment


              #7
              We don't need the (confidential) code of your actual application, but we do need a way to reproduce the problem.

              So if you think you've isolated the behavior to a framework bug, just start from a sample and apply the parser/editor logic that you think should work, and if you re-create the issue, show us that (obviously non-confidential) code.

              Comment


                #8
                Okay. Here is my test case for the previously described issue... (I had to print it out and type it up so please excuse typos)

                Code:
                static Double toDouble(Object o) {
                    return o == null ? null : o instanceof Double ? (Double) o : Double.parseDouble(toString(o));
                }
                
                static String toString(Object o) {
                    return o == null ? null : o instanceof String ? (String) o : o.toString());
                }
                
                CellFormatter toMegaCellFormatter = new CellFormatter() {
                    @Override
                    public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                        // Convert normal to mega unit
                        return Double.toString((value != null) ? toDouble(value) / 1000000 : 0d);
                    }
                }
                
                CellEditValueFormatter toMegaCellEditFormatter = new CellEditValueFormatter() {
                    @Override
                    public Object format(Object value, ListGridRecord record, int rowNum, int colNum) {
                        // Convert normal to mega unit
                        return Double.toString((value != null) ? toDouble(value) / 1000000 : 0d);
                    }
                }
                
                CellEditValueParser fromMegaCellEditParser = new CellEditValueParser() {
                    @Override
                    public Object format(Object value, ListGridRecord record, int rowNum, int colNum) {
                        // Convert from mega to normal unit
                        return (value !=null) ? toDouble(value) * 1000000 : 0d;
                    }
                }
                
                @Override
                public void onModuleLoad() {
                    ListGridField field1 = newListGridField("field1");
                    field1.setDefaultValue(3000000);
                
                    ListGridField field2 = newListGridField("field2");
                    field1.setDefaultValue(5000000);
                
                    final ListGrid grid = new ListGrid();
                    grid.setSize("400", "400");
                    grid.setFields(field1, field2);
                
                    IButton addButton = new IButton("Add");
                    addButton.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent e) {
                            grid.startEditingNew();
                        }
                    }
                
                    IButton clearButton = new IButton("Clear");
                    clearButton.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent e) {
                            grid.setData(new Record[0]);
                        }
                    }
                
                    VLayout layout = new VLayout();
                    layout.setMembers(grid, addButton, clearButton);
                    layout.draw();
                }
                
                ListGridField newListGridField(String name) {
                    ListGridField field = new ListGridField(name);
                    field.setRequired(true);
                    field.setCellFormatter(toMegaCellFormatter);
                    field.setEditValueFormatter(toMegaCellEditFormatter);
                    field.setEditValueParser(fromMegaCellEditParser);
                    field.setType(ListGridFieldType.FLOAT);
                    return field;
                }

                Comment


                  #9
                  Since the original posts are pretty old - what version does this apply to?

                  And just to verify - have you run this code on it's own (not in your large app)?

                  Comment


                    #10
                    2.5p and yup, on it's own.

                    Comment


                      #11
                      Sorry to belabor the point, but by 2.5p and up, do you mean that a fix to a more recent version (say, 3.1) is fine for you?

                      The reason we're asking is that this looks very obscure, yet may end up being due to a native browser bug, hence hard to fix. If so, we probably would not fix it for an older version unless you absolutely need it.

                      Comment


                        #12
                        I meant "yup" as in "yes" :)

                        But to answer your question, we're stuck with 2.5p for now. It would be really nice if the fix could somehow be ported back, if possible.

                        Comment


                          #13
                          OK, we'll see if we can reproduce the problem and see what's required for a fix.

                          Comment


                            #14
                            We've made a change that should address this

                            Please try the next nightly build (Nov 3 or greater) and let us know if you continue to see it

                            Regards
                            Isomorphic Software

                            Comment


                              #15
                              Thanks for the fast turnaround. Would it be possible for me to put the fix into 2.5p somehow?

                              Comment

                              Working...
                              X