سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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";
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top