Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Creating JSF Table Matrix with Pagination



Hi all
i faced an issue how to create a matrix with jsf and also to add pagination
i didn't found it in my goggle search so i am providing the solution here:

well what i wanted to achieve is a 2 dimension table , not item per row but ite, per row & column
the code in the jspx page is:

ice:dataTable id="dataTbl" var="item" value="#{bean.rowDataModel}" rows="2"
ice:columns id="columnDataModel" var="column"
value="#{bean.columnDataModel}"
ice:panelGroup
ice:selectBooleanCheckbox value="#{bean.cellValue.select}"
ice:panelGroup
ice:columns
ice:dataTable

so what we have here is list of 30 items as an example
each row has 3 items
each page has up to 2 rows
the rowDataModel is used in order get row data for jsf and its a list with size 7 for example so we will have 4 pages and each we will have 2 rows .
each row has a column with columnDataModel which is again a list size = 3

now to the backendBean code:
in my bean class i have 2 members-->
private DataModel columnDataModel ;
private DataModel rowDataModel ;

in my constracor i have :
dummyList.add(new Item(false)); -- creating some list with items that i want to show in the matrix i will do it 29 more times
rows = dummyList.size()/columns; -- calculate number of total rows for example 7
generateDataModels(); -- create model for each page

and 2 methods which are invoked from the contractor

private void generateDataModels() {
List columnList =new ArrayList();
List rowList = new ArrayList();

for (int i = 1; i <= rows; i++) {
rowList.add(i);
}
rowDataModel = new ListDataModel(rowList);

for (int j = 1; j <= columns; j++) {
columnList.add(j);
}
columnDataModel = new ListDataModel(columnList);
}

this method will create page model list with 3 columns for columnDataModel and 7 for rowDataModel

in order to get each cell item i am calling

public Item getCellValue() {
if (rowDataModel.isRowAvailable() && columnDataModel.isRowAvailable()){
int column = columnDataModel.getRowIndex();
int row = rowDataModel.getRowIndex();
return dummyList.get((row*columns)+column);
}
}
return null;

}

this method will return me from the dummy list which will have like 30 items
the item per page + per row and column

this is why in the jspx i can do

ice:selectBooleanCheckbox value="#{bean.cellValue.select}"

and to get the value of the specific cell

it may be complicated but it works so good :-)

now to add pagination all i added was

ice:dataPaginator for="dataTbl" paginator="true" paginatorMaxPages="2"
f:facet name="first"
ice:graphicImage id="graphicImageId_1" url="/images/First.png" style="border:none;" title="First Page"
f:facet
f:facet name="last"
ice:graphicImage id="graphicImageId_2" url="/images/Last.png" style="border:none; title="Last Page"
f:facet

and so on...

the result was a table like

1 2 (pagination)
[ cell 0/0 ] [cell 0/1] [cell 0/2]
[cell 1/0 ] [cell 1/1] [cell 1/2]

each cell is an item object that i can get its title or image or price ...

hope it help
Dori Waldman