Summary row in table [message #717030] |
Fri, 19 August 2011 05:55 |
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 12:43 |
|
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 19:49 |
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);
}
}
}
}
}
|
|
|
|
|
Re: Summary row in table [message #1404302 is a reply to message #1404242] |
Thu, 31 July 2014 06:47 |
Lukas Steigerwald Messages: 47 Registered: July 2014 |
Member |
|
|
This is how my SortColum looks like:
@Order(10.0)
public class SortColumn extends AbstractIntegerColumn {
@Override
protected boolean getConfiguredAlwaysIncludeSortAtBegin() {
return true;
}
@Override
protected int getConfiguredSortIndex() {
return 0;
}
}
In my opinion no difference to your code. Do I have to setup something else for the other columns that are not the summary column maybe?
Regarding my setup (same as we had during my last issue):
Scout Version: 4.0
UI: same result in all three clients (focus on RAP and SWT)
Eclipse Platform: 4.4
[Updated on: Thu, 31 July 2014 06:48] Report message to a moderator
|
|
|
|
|
Re: Summary row in table [message #1404356 is a reply to message #1404337] |
Thu, 31 July 2014 13:23 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
I have also a Table Field in a Form.
What occures when you call "Reset columns > Sorting" in the context menu in the Table header.
You can also call table.sort() in your code.
I have manage to produce a situation where this is not working:
@Order(10.0)
public class Table extends AbstractExtensibleTable {
@Override
protected boolean getConfiguredSortEnabled() {
return false;
}
//rest of the table.
}
can you check the "Sort Enabled" property in your table?
(maybe it is not set with the getConfiguredSortEnabled() method but later with setSortEnabled(..)?)
You should debug: AbstractTable#sort().
From what I can tell, every thing works by design.
We should probably contribute some unit test for sorting in
org.eclipse.scout.rt.client.ui.basic.table.TableTest
Are you interested in contributing tests for the framework?
.
[Updated on: Thu, 31 July 2014 13:23] Report message to a moderator
|
|
|
Re: Summary row in table [message #1404402 is a reply to message #1404356] |
Fri, 01 August 2014 06:42 |
Lukas Steigerwald Messages: 47 Registered: July 2014 |
Member |
|
|
Reset columns > Sorting brings the table back to the default like shown in the first screenshot of my last post. Except that in the header column the arrow for sort ascending is shown. And neither in the form nor in the Page opening the form there is any getConfiguredSortEnabled() or setSortEnabled method. Will try to debug the sort() method of AbstractTable() next and hope to find something there. Thank you for your time and suggestions until now.
EDIT: debuggin sort() I noticed that getColumnSet().getSortColumns() only returns the column that I have clicked on in the UI. Where should the sort() method get to know about the "always Include Sort at Begin"-column?
[Updated on: Fri, 01 August 2014 07:04] Report message to a moderator
|
|
|
Re: Summary row in table [message #1404404 is a reply to message #1404402] |
Fri, 01 August 2014 07:05 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
SortEnabled is a property of the Table (inside you TablePage or TableField in your Form). You should override getConfiguredSortEnabled() in the Table inner-class.
Edit: In my case when I click on the Text column to sort "getColumnSet().getSortColumns()" return:
- [0] MyForm$MainBox$MyTableField$Table$SortColumn
- [1] MyForm$MainBox$MyTableField$Table$TextColumn
I have 2 columns in this list...
Can you inspect table.getColumnSet()?
I have:
* m_permanentHeadSortColumns ArrayList<E> (size 1)
- [0] DesktopForm$MainBox$MyTableField$Table$SortColumn
* m_permanentTailSortColumns ArrayList<E> (size 0)
* m_userSortColumns ArrayList<E> (size 1)
- [0] DesktopForm$MainBox$MyTableField$Table$TextColumn
.
[Updated on: Fri, 01 August 2014 07:15] Report message to a moderator
|
|
|
Re: Summary row in table [message #1404408 is a reply to message #1404404] |
Fri, 01 August 2014 07:35 |
Lukas Steigerwald Messages: 47 Registered: July 2014 |
Member |
|
|
Ok...I discovered the following: When starting the form by opening the page there is first no ascending arrow for the sort column. When I hit another column to sort the sort-column is ignored (and the m_permanentHeadSortColumns-Array is empty). However after resetting the columns with the menu I get the ascending arrow on the sort-column. When now sorting another column by clicking on its header everything works as it should ( and table.getColumnSet() has the expected array content, as described in your previous post)
EDIT: Only sorting by the sort column has not the same effect as getting the ascending-arrow by resetting sort; Furthermore after resetting the sort the other column to sort has not the ascending/descending arrow when sorted but [a-z]/[z-a].
[Updated on: Fri, 01 August 2014 07:47] Report message to a moderator
|
|
|
|
|
|
|
|
Re: Summary row in table [message #1404471 is a reply to message #1404429] |
Fri, 01 August 2014 15:04 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
Ok I spend time on writing Unit tests in org.eclipse.scout.rt.client.ui.basic.table.TableTest (diff) to ensure that table-ordering works as I told.
I managed to reproduce the symptoms you are describing (m_permanentHeadSortColumns empty, sort order not considered...).
Is it possible that the Table where you have your sort problem is also the same table that you have also mentioned in this thread: Saving users table customisation?
The fix for my unit tests was to add table.initTable() at the beginning of my unit test. I do not know why but looking at the code, it seems to be necessary. (see as example: TableRowInsertOrderTest)
Can you add a breakpoint in AbstractTable#initTable() and see if you are called? I guess not. It is probably related to your other problem.
Without seeing your code, I can not tell what is wrong.
.
PS: I will be on holidays the major part of august and will not be able to reply.
[Updated on: Fri, 01 August 2014 16:00] Report message to a moderator
|
|
|
Re: Summary row in table [message #1404552 is a reply to message #1404471] |
Mon, 04 August 2014 07:32 |
Lukas Steigerwald Messages: 47 Registered: July 2014 |
Member |
|
|
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 07:40] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04508 seconds