Search code examples
jsfcomponentsconditional-rendering

Conditionally toggle value of component attribute based on a boolean


I'm having a JSF datatable with one column containing a list of artists together with a commandLink for each that I'd like to either show or hide the albums that specific artist has made when you click the link.

I've just started to learn about JSF and I'm wondering what the best practice would be to get the value of the commandLink to change between "Show albums" and "Hide albums" when clicking the link? Is it possible to do this without using javascript?

Thanks


Solution

  • You can use the conditional operator ?: in EL for that. If the boolean expression evaluates true, then the first statement will be executed, otherwise the second statement.

    <h:commandLink ... value="#{bean.showAlbums ? 'Show' : 'Hide'} Albums" />
    

    You could even use the same condition as you're using tho show/hide the actual albums.