Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Summary row in table
Summary row in table [message #717030] Fri, 19 August 2011 01:55 Go to next message
Adrian Moser
Messages: 67
Registered: March 2011
Member
I have a scout table page with numeric column data.
What is the preferred way to create a summary row? (e.g. avg, sum, min, max)

Is there an automatic way? How can I make sure the summary row stays always at the same position (bottom) when sorting?

Kind regards
Adrian
Re: Summary row in table [message #717571 is a reply to message #717030] Sun, 21 August 2011 08:43 Go to previous messageGo to next message
Claudio Guglielmo is currently offline Claudio Guglielmo
Messages: 126
Registered: March 2010
Senior Member
Hi Adrian

To make sure the summary row always stays at the bottom of the table I suggest to create an invisible column and set the property AlwaysIncludeSortAtBegin to true. This makes sure the sort order of this column is always considered first when sorting any column. So the sort order of this column is important which means the value of the summary row needs to be greater than the other values.

Here is an example of the data. The first column is the invisible sort column:

   
@Override
protected Object[][] execLoadTableData(SearchFilter filter) throws ProcessingException {
  return new Object[][]{
      {0, "Company A", 50000},
      {0, "Company B", 10000},
      {1, "Total", 60000},
  };
}


The invisible sort column could look like this:

@Order(10.0)
public class SortColumn extends AbstractIntegerColumn {

  @Override
  protected boolean getConfiguredAlwaysIncludeSortAtBegin() {
     return true;
  }

   @Override
   protected boolean getConfiguredDisplayable() {
     return true;
  }

  @Override
  protected int getConfiguredSortIndex() {  
    return 0;
  }

}


To group the data (sum, avg) etc. I suggest to do this directly in the sql code when selecting the data.

SELECT 0, name, value from company
UNION
SELECT 1, 'Total', sum(value) from company


Best regards
Claudio
Re: Summary row in table [message #717701 is a reply to message #717571] Mon, 22 August 2011 02:57 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie Bresson
Messages: 117
Registered: November 2010
Senior Member
@Claudio Guglielmo

I think you wanted to say that the sort Column should not be Displayable
   @Override
   protected boolean getConfiguredDisplayable() {
     return false;
  }



I think there wasn't the need to compute a Summary Row in Java, but I could be added to the framework... (in a generalized way). This is something I find very comfortable in JasperReport where you can add this kind of operation in a group-band.

To my mind support for tree-table in scout is also missing and it would go in the same direction.



Here some input on the configuration of a the sorting properties in a column:

* SortIndex: configure the sorting priority of the column (the smallest the better).

* SortAscending: if true the rows are sorted in the ascending order (from A to Z or 0 to 9). if false the rows are sorted in the descending order.

* AlwaysIncludeSortAtBegin: if true, the column is always sorted before the user configuration. (priority between all columns Included at begin is still defined with the SortIndex)

* AlwaysIncludeSortAtEnd: if true, the column is always sorted after the user configuration. (priority between all columns Included at end is still defined with the SortIndex)


Please tell me if this is clear enough. I will put this in our wiki.

I have a draft of an "how to" article to explain how sorting is working in scout. I never took time to publish it.
Re: Summary row in table [message #717847 is a reply to message #717701] Mon, 22 August 2011 10:09 Go to previous messageGo to next message
Claudio Guglielmo is currently offline Claudio Guglielmo
Messages: 126
Registered: March 2010
Senior Member
Hi Jeremie

That's ok for me. If you miss any feature please open a change request ticket in bugzilla. Otherwise your idea will be forgotten soon.

Regards
Claudiot
Re: Summary row in table [message #718598 is a reply to message #717847] Wed, 24 August 2011 15:49 Go to previous message
Adrian Moser
Messages: 67
Registered: March 2011
Member
Thanks for your feedback, and the pattern with the sort column.
But, I don't like the idea using SQL for calculation, since you are duplicating code in the where clause.

I tried creating an AbstractSummaryTable, which handles creating the row and doing the calculation. If I need to have a summary row, basically I just need to add the sort column and replace the default AbstractTable with the AbstractSummaryTable.

Any suggestions?

import org.eclipse.scout.commons.BooleanUtility;
import org.eclipse.scout.commons.NumberUtility;
import org.eclipse.scout.commons.exception.ProcessingException;
import org.eclipse.scout.rt.client.ui.basic.table.AbstractTable;
import org.eclipse.scout.rt.client.ui.basic.table.ITableRow;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractBooleanColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractDoubleColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractLongColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractStringColumn;
import org.eclipse.scout.rt.client.ui.basic.table.columns.IColumn;
import org.eclipse.scout.rt.shared.data.basic.FontSpec;

public abstract class AbstractSummaryTable extends AbstractTable {

  protected abstract Class<? extends AbstractBooleanColumn> getConfiguredSummaryOrderColumn();

  protected abstract Class<? extends AbstractStringColumn> getConfiguredSummaryTotalColumn();

  @Override
  protected final void execDecorateRow(ITableRow row) throws ProcessingException {

    // create summary row
    if ((getTableData().length - 1) == row.getRowIndex() &&
        !BooleanUtility.nvl(getColumnSet().getColumnByClass(getConfiguredSummaryOrderColumn()).getValue(row), false)) {
      ITableRow newRow = createRow();
      getColumnSet().getColumnByClass(getConfiguredSummaryOrderColumn()).setValue(newRow, true);
      addRow(newRow);
    }

    // populate summary row
    if (BooleanUtility.nvl(getColumnSet().getColumnByClass(getConfiguredSummaryOrderColumn()).getValue(row), false)) {
      // bold
      row.setFont(FontSpec.parse("bold"));
      row.setIconId(null);

      // label
      getColumnSet().getColumnByClass(getConfiguredSummaryTotalColumn()).setValue(row, Texts.get("Total"));

      // summary
      int dataColCount = getRowCount() - 1;
      for (IColumn column : getColumnSet().getColumns()) {

        // Long
        if (column instanceof AbstractLongColumn) {
          Long sum = 0L;
          for (int k = 0; k < dataColCount; k++) {
            sum += NumberUtility.nvl((Long) getTableData()[k][column.getColumnIndex()], 0);
          }
          ((AbstractLongColumn) column).setValue(dataColCount, sum);
        }

        // Double
        if (column instanceof AbstractDoubleColumn) {
          Double sum = 0D;
          for (int k = 0; k < dataColCount; k++) {
            sum = sum + NumberUtility.nvl((Double) getTableData()[k][column.getColumnIndex()], 0D);
          }
          ((AbstractDoubleColumn) column).setValue(dataColCount, sum);
        }
      }
    }
  }

}

Previous Topic:AbstractTable: Dynamic Columns
Next Topic:(Really) dynamic form fields?
Goto Forum:
  


Current Time: Sat Apr 19 09:06:26 EDT 2014

Powered by FUDForum. Page generated in 0.02261 seconds