How to get row index in dynamically populated table?

Hi,

I am following example given in http://balusc.xs4all.nl/srv/dev-jep-dat.html to Dynamically populate datatable. How can I get row index while populating table in populateDynamicDataTable() method

private List myList;

private String[] headers; // Optional.

private HtmlDataTable dynamicDataTable;

// Actions -

public void loadMyList() {

// Set headers (optional).

headers = new String[] {"header1", "header2", "header3"};

// Set rows. This is a stub example, just do your dynamic thing.

String[] row1 = {"ID1", "Name1", "Value1"};

String[] row2 = {"ID2", "Name2", "Value2"};

String[] row3 = {"ID3", "Name3", "Value3"};

// Convert rows to List and set the List.

myList = new ArrayList();

myList.add(Arrays.asList(row1));

myList.add(Arrays.asList(row2));

myList.add(Arrays.asList(row3));

}

public void populateDynamicDataTable() {

//*********************** I want current row in this method *************//

// Any columns?

if (myList != null && myList.size() > 0) {

dynamicDataTable = new HtmlDataTable();

// Get amount of columns.

int columns = ((List) myList.get(0)).size();

// Set columns.

for (int i = 0; i < columns; i++) {

// Set header (optional).

UIOutput header = new UIOutput();

header.setValue(headers);

// Set output.

UIOutput output = new UIOutput();

ValueBinding myItem =

FacesContext

.getCurrentInstance()

.getApplication()

.createValueBinding("#{myItem[" + i + "]}");

output.setValueBinding("value", myItem);

// Set column.

UIColumn column = new UIColumn();

column.setHeader(header);

column.getChildren().add(output);

// Add column.

dynamicDataTable.getChildren().add(column);

}

}

}

// Getters -

public List getMyList() {

return myList;

}

public HtmlDataTable getDynamicDataTable() {

if (dynamicDataTable == null) {

loadMyList(); // Reload to get most recent data.

populateDynamicDataTable();

}

return dynamicDataTable;

}

// Setters -

public void setMyList(List myList) {

this.myList = myList;

}

public void setDynamicDataTable(HtmlDataTable dynamicDataTable) {

this.dynamicDataTable = dynamicDataTable;

}

}

I have tried dynamicDataTable.getRowIndex, but it returns -1

Is there any other way?

Thanks ,

Chitra.

[2576 byte] By [chitra_Ja] at [2007-11-27 6:33:15]
# 1
> get row indexWhy?
BalusCa at 2007-7-12 17:59:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Depending upon the row Index, I want to set some style attributes of outputText in column.I want to highlight some cells in a table, for that I need rowIndex.
chitra_Ja at 2007-7-12 17:59:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

When you dynamically populate a datatable, you're populating the columns, not the actual rows.

Just add EL to the styleClass attribute, like:styleClass="#{myBean.myTable.rowIndex == 1 ? 'highlightedClass' : 'defaultClass'}"

where 'myTable' refers to a HtmlDataTable property in the backing bean.

BalusCa at 2007-7-12 17:59:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks BalusC for your reply, it helped me.
chitra_Ja at 2007-7-12 17:59:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...