Summary row in table [message #717030] |
Fri, 19 August 2011 01:55  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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 #718598 is a reply to message #717847] |
Wed, 24 August 2011 15:49   |
Eclipse User |
|
|
|
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);
}
}
}
}
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Summary row in table [message #1404552 is a reply to message #1404471] |
Mon, 04 August 2014 03:32  |
Eclipse User |
|
|
|
Adding the initTable() directly after creating the TableForm did it. Now the sorting works as expected and the summary row and the empty row stay where they should. And yes, the table is the same one mentioned in my other question. Thank you very much and enjoy your holiday.
EDIT: It seems the initTable() solved the issue with saving a changed order of colums as described in my other question as well. I will add a link to this thread as a solution.
[Updated on: Mon, 04 August 2014 03:40] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.06078 seconds