2

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
nivis
  • 913
  • 3
  • 17
  • 34

2 Answers2

4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • When I tried this it updates all links in the datatable, not just the one I clicked. The idea is that when you click a link, only that link shall change value to "Hide albums" and only the albums for that specific artist should be shown. – nivis Apr 20 '12 at 07:37
  • Then you should let the condition depend on the current row, not on the parent bean, otherwise it will obviously be applied to all rows. E.g. `value="#{artist.showAlbums ? 'Show' : 'Hide'} Albums"` – BalusC Apr 20 '12 at 12:14
  • I've implemented that and it works perfectly. Thank you very much for taking your time to answer my question! – nivis Apr 20 '12 at 12:53
  • @BalusC: Do you know how to access locals (local config) inside the quotation marks? `value="#{controller.myBoolVal ? 'msg.yes' : 'msg.no'}"` does not work, unfortunatelly. – John May 13 '16 at 09:38
  • @John: Eh? You mean resource bundle? Just remove the quotes if those values do not represent strings but EL variables. – BalusC May 13 '16 at 09:54
  • @BalusC: No, I mean internationalization. I defined a resource-bundle in my faces-config.xml and created `.properties`-files for my languages. What I am trying to do, is to display yes and no depending on the user language. – John May 13 '16 at 12:34
  • @John: Ah you meant to say "locale" where you said "local(s)". Those are two very different terms. Well, the answer remains. Just remove those quotes from resource bundle variables. You also don't quote variables in Java as if they're strings, right? – BalusC May 13 '16 at 12:36
0

Your link

<a4j:commandLink value="#{myBean.value}" action="#{myBean.toggleValue}" reRender="myLink" id="myLink"/>

Your bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="myBean")
@ViewScoped
public class MyBean {
  boolean show = true;

  public void toggleValue() {
    this.show = !this.show;
  }

  public String getValue() {
    return this.show ? "Show" : "Hide";
  }
}
prageeth
  • 7,159
  • 7
  • 44
  • 72
  • Thank you for answering! When I tried this it updates all links in the datatable, not just the one I clicked. The idea is that when you click a link, only that link shall change value to "Hide albums" and only the albums for that specific artist should be shown. – nivis Apr 20 '12 at 07:37