Announcement

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

    ListGrid with float cell and cellformatter

    Hello, i have a weird situation i can't get my head around.

    I have a listgrid with a lot of float columns in the datatype like this:

    Code:
     <field name="d6" type="float" title="6" required="true" format="0.00"/>
    All works well, it prints 8.76, 3.15, and 0.00 for zero. However, I want the fields that are zero to be blanked out, so i'm trying to add a cellformatter

    my code:
    Code:
    CellFormatter setZeroBlankFormatter = new CellFormatter() {
        @Override
        public String format(Object value, ListGridRecord listGridRecord, int i, int i1) {
            System.out.println("Formatting "+ value.toString() + ", type: " + value.getClass());//this is just me trying since i cant get it to work
            return ""+o;
        }
    };
    ListGridField[] fields = grid.getFields();
    for (ListGridField field : fields) {
        if(field.getName().startsWith("d")){
            field.setCellFormatter(setZeroBlankFormatter);
        }
    }
    The problem is that the object o that gets called in the formatter is now an integer! so now all cells are 0, 1 etc. I would have expected the formatter to be called with a float object.

    Am i doing something wrong?

    EDIT: From the Cellformatter javadoc: "value - raw value for the cell, from the record for the row". This appears to not be the case, since the class of the value is int even though the datatype is float.

    EDIT2: It seems as though the class of the value differs whether the value is a fraction or a whole value! So, if it's 8.5 its a double, if its 8.0 its an int. This does not match the javadoc? Nor what i would expect.
    It also makes the cellformatter harder to write since i basically must check for the class for each cell in order to be able to do formatting.
    Last edited by mathias; 24 Apr 2018, 00:48.

    #2
    This is how i ended up doing it. It works, but it's pretty ugly. Would be great with some feedback to see if this is working as intended (regarding the float/int type variation) and whether there's a better way.

    Code:
    private static final NumberFormat twoDecimalNumberFormat = NumberFormat.getFormat("########0.00");
    .....
    CellFormatter setZeroBlankFormatter = new CellFormatter() {
        @Override
        public String format(Object o, ListGridRecord listGridRecord, int i, int i1) {
            if(o.getClass() == Integer.class){
                if(((int)o) == 0){
                    return "";
                }
                return twoDecimalNumberFormat.format((int)o);
            }
            return twoDecimalNumberFormat.format((double) o);
        }
    };
    ListGridField[] fields = grid.getFields();
    for (ListGridField field : fields) {
        if(field.getName().startsWith("d")){
            field.setCellFormatter(setZeroBlankFormatter);
        }
    }

    Comment


      #3
      Are you saying that when the value is null, you receive it as an Integer with numeric value 0? We don't seem to be reproducing that; if you can't reproduce it by modifying a Showcase sample, perhaps the problem is somewhere in how you are delivering data to your app.

      Comment


        #4
        Hi, no my data is never null, if it's zero i send a double 0.0 from the server.

        What i am saying is that the "format()" method is called with Object values of different classes depending on the double values sent from the server:

        The method is called with an Integer object if the value sent from the server is any whole number (0.0, 1.0, 2.0 etc) and a double if it is a fraction (1.23, 1.45 etc).
        I would have expected it to be the same type of object at all times regardless of the actual value, especially since i define it as a float in the Datasource definition.

        Comment


          #5
          I checked with firebug, and, again, the datasource xml definition looks like i showed above (i.e. all fields have type="float"), this is is how its sent to the client:

          Code:
           
           data:[{d10:0,d12:0,d11:3,d14:4.31,d13:0,d16:0,d15:5.5,d18:0,d17:0,d19:0,d21:0,d20:0,d23:0,d22:0,d25:0,d24:0,d27:0,d26:0,d29:0,d28:0,d30:0,d31:0,
          As a sidenote, i tried calling setdatatype to float manually on the listgridfield in the client, but it had no effect.

          Comment


            #6
            We've fixed CellFormatter and a few other APIs in SGWT 6.1p and newer releases to respect the declared type of the ListGridField - if specified as "float" or "integer". Now you should always get a Double if the field is specified as "float". If the field doesn't have a numeric type (e.g. "text") but the underlying JavaScript value is nevertheless a Number, for backcompat the old logic which picks a Java Number subtype based on the actual value will still run.

            The fix will be in the nightly builds dated 2018-05-18 and beyond

            Comment


              #7
              Allright, thanks. Will take a peek asap.

              Comment

              Working...
              X