Displaying a list of object in a table with multiple columns
Hy,
I would like to know how can I display a list o object like Images in table with different columns.
I have a List<Image> which I pass to the h:datatable .But I didn't found the solution to display this list in for example two columns because for the columns I can just pass the Image element not a precised one accessed by his index in the list.
<h:dataTable headerClass="list-header" id="test" value="#{manager.images}" var="image" width="648">
<h:column>
<webuijsf:button imageURL="#{image.url}" />
</h:column>
<h:column>
</h:dataTable>
What I would like to do is to display the images in multiple columns.
But I don't found how to do.
Thanks in advance.
Message was edited by:
WebService
# 1
Use a h:panelGrid with a specified number of columns and dynamically pass the list through it.
Basic example:
JSF<h:panelGrid binding="#{myBean.grid}" columns="2" />
MyBeanprivate HtmlPanelGrid grid = new HtmlPanelGrid(); // + getter + setter
// Prefill the grid in initialization block.
{
loadImagesIntoGrid();
}
// Or in the constructor.
public MyBean() {
loadImagesIntoGrid();
}
// Or in the getter.
public HtmlPanelGrid getGrid() {
loadImagesIntoGrid();
return grid;
}
private void loadImagesIntoGrid() {
List<Image> images = getImagesSomehow();
for (Image image : images) {
// Do your thing to create the webuijsf button, this is a SUN JSF RI example, I know nothing about webuijsf components.
HtmCommandButton button = new HtmlCommandButton();
button.setImage(image.getURL());
// Don't forget to set the button action, this button would be useless otherwise.
// Add to grid.
grid.getChildren().add(button);
}
}
# 2
Thank you for your response .
That was really helpful for me.
I m sorry but I have some others questions.
How can I please set the button action.Because I use this also.Im really new with JSF.
How to specify the space between the displayed images.So between the columns.
And how can I specify the size of the displayed image on the button.
Thanks in advance.
# 6
Those are available for h:dataTable, not for h:panelGrid. Write your own. It's fairly simple to implement using at least 2 buttons in the h:panelGrid footer indicating previous and next page. The "Paging datatable" paragraph in this article might provide useful insights: http://balusc.xs4all.nl/srv/dev-jep-dat.html
Then you can use the current row and the rowcount in the List#subList() of the images list. You may need to cache images during paging instead of reloading them everytime to save performance. Or implement the subset in the SQL query, some DB's supports it.