Table with dynamic number of columns [message #889307] |
Tue, 19 June 2012 05:34  |
Eclipse User |
|
|
|
Hello everyone
In my application, I need to create a form describing a set of „changes". Usually those are describe in a „Name-Value" like style.
Until now, I used a table with a single column (String column with HTML) to describe the changes, using multiline-cells. Something like this:
Event name: Birthday party
Location: Grand Central Park
Duration: 180 minutes
Event name: Funeral
Location: Grand Central Cemetery
Duration: 60 minutes
Now the question came up whether we could transform this into a horizontal form (i.e. a table with more than one column).
While one set of changes would have the same number of columns, another set may have a larger (or smaller) number of columns.
Until now, I just had a fixed number of columns or made as many columns as one might need (and just hide the ones that are not necessary).
But in general (or for an unknown number of columns, is it possible in Scout to tell a table at runtime to create N columns which we can then fill?
Thanks!
|
|
|
|
Re: Table with dynamic number of columns [message #889381 is a reply to message #889307] |
Tue, 19 June 2012 07:19   |
Eclipse User |
|
|
|
This is an example:
import java.util.ArrayList;
import java.util.List;
import org.eclipse.scout.commons.annotations.Order;
import org.eclipse.scout.commons.exception.ProcessingException;
import org.eclipse.scout.rt.client.ui.basic.cell.Cell;
import org.eclipse.scout.rt.client.ui.basic.table.AbstractTable;
import org.eclipse.scout.rt.client.ui.basic.table.HeaderCell;
import org.eclipse.scout.rt.client.ui.basic.table.ITableRow;
import org.eclipse.scout.rt.client.ui.basic.table.columns.AbstractColumn;
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.IColumn;
import org.eclipse.scout.rt.client.ui.desktop.outline.pages.AbstractPageWithTable;
import org.eclipse.scout.rt.client.ui.desktop.outline.pages.ISearchForm;
import org.eclipse.scout.rt.shared.TEXTS;
import org.eclipse.scout.rt.shared.data.basic.FontSpec;
import org.eclipse.scout.rt.shared.services.common.jdbc.SearchFilter;
import org.eclipse.scout.service.SERVICES;
public class DynamicColumnsTablePage extends AbstractPageWithTable<DynamicColumnsTablePage.Table> {
private List<IColumn<?>> m_injectedColumns;
@Override
protected String getConfiguredIconId() {
return Icons.TreeNode;
}
@Override
protected boolean getConfiguredLeaf() {
return true;
}
@Override
protected Class<? extends ISearchForm> getConfiguredSearchForm() {
return DynamicDataSearchForm.class;
}
@Override
protected boolean getConfiguredSearchRequired() {
return true;
}
@Override
protected String getConfiguredTitle() {
return TEXTS.get("DynamicColumns");
}
@Override
protected Object[][] execLoadTableData(SearchFilter filter) throws ProcessingException {
DynamicDataSearchFormData formData = (DynamicDataSearchFormData) filter.getFormData();
DynamicDataMatrixData matrixData = SERVICES.getService(IDynamicDataPageService.class).getStatisticsMatrixData(formData);
if (matrixData == null) {
return new Object[0][];
}
//create dynamic columns based on matrix data assuming matrixData has a property "getColumnSpecs" with bean ColumnSpec
updateDynamicColumns(matrixData.getColumnSpecs());
//fill rows
return matrixData.getRows();
}
private void updateDynamicColumns(ColumnSpec[] columnSpecs) throws ProcessingException {
if (columnSpecs.length == 0) {
return;
}
Table table = getTable();
m_injectedColumns = new ArrayList<IColumn<?>>();
for (ColumnSpec spec: columnSpecs) {
//XXX switch on spec.getType() or something like that...
m_injectedColumns.add(createDynamicDoubleColumn(spec.getId(), spec.getText()));
//...
}
table.resetColumnConfiguration();
}
private IColumn<?> createDynamicDoubleColumn(final String columnId, final String label) {
return new AbstractDoubleColumn() {
@Override
protected String getConfiguredHeaderText() {
return label;
}
@Override
public String getColumnId() {
return columnId;
}
@Override
protected int getConfiguredWidth() {
return 120;
}
@Override
protected Double execParseValue(ITableRow row, Object rawValue) throws ProcessingException {
rawValue = preprocessDynamicColumnsCell(this, row, rawValue);
return super.execParseValue(row, rawValue);
}
};
}
/**
* @return {@link DecoratedValue#getValue()} after decorating the table cell
*/
private Object preprocessDynamicColumnsCell(IColumn<?> col, ITableRow row, Object rawValue) throws ProcessingException {
DecoratedValue rawCell = (DecoratedValue) rawValue;
if (rawCell != null) {
row.getCellForUpdate(col).setBackgroundColor(rawCell.getColor());
return rawCell.getValue();
}
return rawValue;
}
@Order(10.0f)
public class Table extends AbstractTable {
@Override
protected boolean getConfiguredSortEnabled() {
return false;
}
@Override
protected void injectColumnsInternal(List<IColumn<?>> columnList) {
if (m_injectedColumns != null) {
columnList.addAll(m_injectedColumns);
}
}
}
}
|
|
|
|
Re: Table with dynamic number of columns [message #1726861 is a reply to message #889381] |
Thu, 17 March 2016 06:26   |
Eclipse User |
|
|
|
Hi Ivan,
Great Example...
It would be great if complete source of this example is posted?, please. Also please share class definitions for DynamicDataMatrixData, Columnspec, DecoratedValue, and its IDynamicDataPageService.
Thanks in advance.
Regards,
Vijay
|
|
|
|
Powered by
FUDForum. Page generated in 0.03969 seconds