views:

1676

answers:

3

Hi,

I have been trying to achieve this functionality of expand/collapse of table rows using core JSF and also I have to preserve the sorting. Is there a way in core JSF where I can achieve this functionality?

A: 

I don't think you can do this with 'core' JSF (by which I assume you mean using only the reference implementation).

As I understand it, you can accomplish subtables with RichFaces subTable

You can also do a similar thing with IceFaces - see the component showcase (it's under Table -> Expandable Table). However either of these would require you adding and setting up another library (RichFaces or IceFaces) which is probably not what you want.

Phill Sacre
+2  A: 

If you insist in using reference implementation only, then you can't go around using a nested h:dataTable and/or h:panelGroup and a good shot of CSS to get it aligned nicely. You can then use JavaScript the smart way to show/hide row details.

Here's a basic kickoff example:

<h:dataTable value="#{bean.orders}" var="order">
    <h:column>
        <h:panelGrid columns="3">
            <h:graphicImage id="expand" value="expand.gif" onclick="toggleDetails(this);" />
            <h:outputText value="#{order.id}" />
            <h:outputText value="#{order.name}" />
        </h:panelGrid>
        <h:dataTable id="details" value="#{order.details}" var="detail" style="display: none;">
            <h:column><h:outputText value="#{detail.date}" /></h:column>
            <h:column><h:outputText value="#{detail.description}" /></h:column>
            <h:column><h:outputText value="#{detail.quantity}" /></h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

The toggleDetails() function can look like (note that it takes JSF generated client ID into account):

function toggleDetails(image) {
    var detailsId = image.id.substring(0, image.id.lastIndexOf(':')) + ':details';
    var details = document.getElementById(detailsId);
    if (details.style.display == 'none') {
        details.style.display = 'block';
        image.src = 'collapse.gif';
    } else {
        details.style.display = 'none';
        image.src = 'expand.gif';
    }
}
BalusC
Is it possible to sort the column
Nrusingha
Truly it is possible. You can find an example here: http://balusc.blogspot.com/2006/06/using-datatables.html#SortingDatatable
BalusC
I am new to JSF, just wondering if I can implement the sorting for panel grid as well as the nested table inside the panel grid. The sorting algorithm should short all the elements in panelGrid as well as in datatable.
Nrusingha
Yes, you can, let a button do the desired `Collections#sort()` thing on the list and then just redisplay.
BalusC
A: 

Simple expandable table with RichFaces

We can do Simple expandable table with RichFaces and the code is given below...

Wonder Location Image

Sabu