Announcement

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

    Hilite in ListGrid based on Derived (Calculated) Fields

    I am trying to apply a Hilite to a ListGrid based on a (calculated) value. The code is shown below. The commented lines show ways I have tried to do this.

    It seems like I cannot use the derived field in the criterion since it does not properly hilite when this field is used to specify the criterion.

    I also tried to just repeat the calculation in the criterion but it looks like the 3rd argument creating the criterion cannot be the name of a field and must be a literal value.

    Any suggestions on a way of applying the Hilite as attempted in the code below?

    Thanks in advance!

    Code:
    public class TestTable extends ListGrid
    {
       private static Hilite[] hilites = new Hilite[] {
          new Hilite() {{
            setFieldNames("ACTUAL", "EXPECTED", "MARGIN");
           setCriteria(new Criterion("MARGIN", OperatorId.LESS_OR_EQUAL, 1000.0f));
    //        setCriteria(new Criterion("ACTUAL", OperatorId.LESS_OR_EQUAL, "EXPECTED"));
            setTextColor("#3333FF");
            setId("0");
          }},
          new Hilite() {{
             setFieldNames("PG_LATENCY","EXPECTED","MARGIN");
             setCriteria(new Criterion("MARGIN", OperatorId.GREATER_THAN, 1000.0f));
    //         setCriteria(new Criterion("ACTUAL", OperatorId.GREATER_THAN, "EXPECTED"));
             setTextColor("#FF0000");
             setId("1");
           }}
       };
       
       
       public TestTable()
       {      
          //Setup the information needed for interacting with the DataSource
          DataSource productTimingDataSource = DataSource.get("TestDataSource");
          this.setDataSource(TestDataSource);
          this.setAutoFetchData(true);
    
          //Set the Table to allow basic filtering in a small menu above the table.
          this.setShowFilterEditor(true);
    
          //Create ListGrid Fields for each of the fields you want from the DataSource
          List<ListGridField> listGridFields = new ArrayList<ListGridField>();
          for(String fieldNameFromDataSource : productTimingDataSource.getFieldNames())
          {
             listGridFields.add(new ListGridField(fieldNameFromDataSource)); 
          }
    
          //Create a derived field for the ListGrid.  This field does not exist directly
          //in the DataSource, but is calculated from the DataSource fields.
          ListGridField calculatedField = new ListGridField("MARGIN", "Margin");
          calculatedField.setCellFormatter(new CellFormatter()
          {
    
             @Override
             public String format(Object value, ListGridRecord record, int rowNum, int colNum)
             {            
                double actual = record.getAttributeAsDouble("ACTUAL");
                double expected = record.getAttributeAsDouble("EXPECTED");
                double margin = expected - actual;               
                
                return String.valueOf(margin);
             }
          });
          listGridFields.add(calculatedField);
    
    
          //Add both the datasource and derived fields to the ListGrid.
          ListGridField[] listGridFieldsAsArray = (ListGridField[]) listGridFields.toArray(new ListGridField[listGridFields.size()]);
          
          this.setFields(listGridFieldsAsArray);
    
          this.setHilites(hilites);
    
       }
    }
    I am using SmarGWT Power Edition 2.5 on FireFox.

    #2
    While this isn't the ideal solution one way of fixing this was to add the calculation of the derived field to the sql DataSource and removing the calculation of the derived field from the ListGrid.

    Once that happened the code above for defining the Hilites seems to work as expected. This does seem to point out that trying to Hilite using a field derived in the ListGrid does not behave as expected.

    As for the commented out line where I attempted to recalculate the margin in the criterion the proper way would have been to call Operator.GREATER_THAN_FIELD instead of Operator.GREATER_THAN.

    Comment


      #3
      The hilite acts on the Records, not on the formatted value. So you can use hilites on derived values, but they do need to be calculated either server side (SQL or Java) or actually stored in the Records via setAttribute(), before the data is provided to the ListGrid (eg, in ListGrid.transformResponse).

      Comment

      Working...
      X