Hi Jarin,

To use x:columns you need to have to have a second collection (contains
the index for each element of your nested arraylist) which contains the
identifier for each column. You can use this collection for the value
attribute in the x:columns component. Define a var in x:columns like the
var for each row in the datatable to determine the current column. The
next step would be to implement a getter method in a bean and take the
current row and the current column to determine which value you want to
show.

Here is an example which uses nested lists to solve your case:

The jsp code:

<x:dataTable value="#{data.rows}" var="row" >
  <x:columns value="#{data.columns}" var="column">
    <h:outputText value="#{data.columnValue}" />
  </x:columns>
</x:dataTable>

The java code for bean data:

Public class Data
{
  private List mRows;
  private List mColumns;

  List getRows()
  {
    if(mRows == null)
    {
      // initialize your data with the nested arraylists
      mRows = initializeData();
    }    
    return mRows;
  }
  
  List getColumns()
  {
    if(mColumns == null)
    {
      mColumns = initializeColumns();
    }
    return mColumns;
  }
  
  List initializeColumns()
  {
    // 1. determine how many columns you have
    int count = 10;
    // 2. create a list which contains the index for each column
    List result = new ArrayList();
    for(int i = 0; i < count; i++)
    {
      result.add(new Integer(i));
    }
    return result;
  }
  
  Object getColumnValue()
  {
    // 1. determine the current row data which is of type List in your
case
    List rowData =
(List)getValueBinding("row").getValue(FacesContext.getCurrentInstance())
;
    
    // 2. determine the current value of the column "row"
    Integer currentColumnIndex =
(Integer)getValueBinding("column").getValue(FacesContext.getCurrentInsta
nce());
    
    // 3. get the n-th element of you rowdata list
    return rowData.get(currentColumnIndex);
  }
}

You can try to use complex objects in the columns list to show a
meaningfull header label for each column

Mathias

> -----Original Message-----
> From: Jaroslav Rychna [mailto:[EMAIL PROTECTED] 
> Sent: Sunday, May 15, 2005 1:01 PM
> To: MyFaces Discussion
> Subject: x:columns
> 
> 
> Hi,
> i have a table with variable number of columns. In my case it 
> means that
> if user selects table1 it has 10 rows and in each row there is 20 
> columns or if user select table2 it has 15 rows and in each 
> row there is 
>   15 columns. There can be many tables and many options, I do 
> not know 
> exact numbers.
> 
> I want to do this by x:dataTable and x:columns. But I don't know how.
> I have ArrayList (the whole table) which consists of 
> ArrayLists (rows) 
> and in this second ArrayList there are columns. (It's like a matrix).
> 
> I looked at example of crosstable, but I'm not sure, that I'm 
> understand it.
> 
> Any thougths how can I do this?
> 
> thanks jarin
> 
> 

Reply via email to