views:

607

answers:

1

I'm looking for a way to show/hide an arbitrary RichFaces component. In this case, I have a <rich:dataTable> that contains several rows. Each row needs to have it's own, independent Show/Hide link, such that when you click "Show details", two things happen:

  1. The "Show details" link is re-rendered as "Hide details"
  2. The associated detailsColumns should become visible (starting from a state of rendered="true" but style="display: none;").

I don't want to write my own JavaScript functions if it's not absolutely necessary. I also don't want to have a server-side bean keep track of which detailColumns are being displayed, and subsequently re-render everything over AJAX: this should be purely client-side behavior. I'm not sure how to accomplish that.

The following pseudo-code (hopefully) illustrates my goal:

<rich:column>
    <a href="#" onclick="#{thisRow.detailsColumn}.show();" rendered="">Show details</a>
    <a href="#" onclick="#{thisRow.detailsColumn}.hide();" rendered="">Hide details</a>
</rich:column>

<rich:column>
    <h:outputText value="#{thisRow.someData}" />
</rich:column>

<rich:column id="detailsColumn" colspan="2" breakBefore="true">
    <h:outputText value="#{thisRow.someMoreData}" />
</rich:column>

+3  A: 

To the point, you need to grab the generated HTML element from the DOM in JavaScript and then toggle its CSS display property between block and none. As far as I know, RichFaces doesn't provide out-of-the-box scripts/facilities for this, but it is basically not that hard:

function toggleDetails(link, show) {
    var elementId = determineItSomehowBasedOnGenerated(link.id);
    document.getElementById(elementId).style.display = (show ? 'block' : 'none');
}

with

<h:outputLink onclick="toggleDetails(this, true); return false;">show</h:outputLink>
<h:outputLink onclick="toggleDetails(this, false); return false;">hide</h:outputLink>
BalusC
+1. You can also use the in-built Richfaces jQuery `rich:jQuery` to do the similar thing very easy.
Shervin
I found this answer to be nearly useless... especially with the call to `determineItSomehowBasedOnGenerated(link.id)`, but thanks for playing.
Dolph
Did you check the generated HTML source? Don't you see patterns in generated ID's? Make use of it! :) It's no rocket science. Substring, split, append, etc on the separator character `:`. I simply couldn't answer that in detail because the lack of information in your question and I don't do RichFaces so I couldn't tell its generated HTML from top of head. I thought it was straightforward enough for you. Nevertheless, you can find another example in [this answer](http://stackoverflow.com/questions/2133985/expand-collapse-of-table-rows-in-datatable-jsf/2134378#2134378) :)
BalusC
In the future, just comment to ask for clarification a bit sooner instead of one month later with a mad smell. I don't shy away from updating the answer with an extended explanation/example how to achieve that :)
BalusC