Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Table with dynamic number of columns
Table with dynamic number of columns [message #889307] Tue, 19 June 2012 05:34 Go to next message
Patrick Baenziger is currently offline Patrick BaenzigerFriend
Messages: 96
Registered: September 2011
Member
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 #889338 is a reply to message #889307] Tue, 19 June 2012 06:19 Go to previous messageGo to next message
Adrian MoserFriend
Messages: 67
Registered: March 2011
Member
Have a look at AbstractTableCustomizer.
If you override injectCustomColumns(), you can add columns to the list at runtime.

You need to add your TableCustomizer to your table by overriding
public ITableCustomizer getTableCustomizer()


Hope that helps
Adrian

[Updated on: Tue, 19 June 2012 06:23]

Report message to a moderator

Re: Table with dynamic number of columns [message #889381 is a reply to message #889307] Tue, 19 June 2012 07:19 Go to previous messageGo to next message
Ivan Motsch is currently offline Ivan MotschFriend
Messages: 154
Registered: March 2010
Senior Member
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);
      }
    }
  }

}
icon14.gif  Re: Table with dynamic number of columns [message #889480 is a reply to message #889307] Tue, 19 June 2012 09:19 Go to previous messageGo to next message
Patrick Baenziger is currently offline Patrick BaenzigerFriend
Messages: 96
Registered: September 2011
Member
Great, thank you both for your speedy replies!
Re: Table with dynamic number of columns [message #1726861 is a reply to message #889381] Thu, 17 March 2016 06:26 Go to previous messageGo to next message
Vijayachandra kumar ramamurthy is currently offline Vijayachandra kumar ramamurthyFriend
Messages: 7
Registered: December 2015
Junior Member
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
Re: Table with dynamic number of columns [message #1862403 is a reply to message #1726861] Fri, 01 December 2023 16:05 Go to previous message
Oueslati Anis is currently offline Oueslati AnisFriend
Messages: 128
Registered: June 2014
Location: Paris
Senior Member
This is very interesting could you please share the complete code exemple with us
Previous Topic:Refresh a listbox with a LookupCall backend
Next Topic:Concurrent access to Scout application
Goto Forum:
  


Current Time: Fri Mar 29 12:33:25 GMT 2024

Powered by FUDForum. Page generated in 0.02160 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top