简体   繁体   中英

How can I show/hide a JSF datatable column with ajax?

I have an ajax call that refreshes the following table. Can I have the "Score" column (it is a BigDecimal) hidden if all values are null, otherwise it should display?

<h:dataTable id="theTable" value="#{MyBean.people}" var="person">
  <h:column>
    <f:facet name="header">
      <h:outputText value="Name" />
    </f:facet>
    #{person.name}
  </h:column> 
  <h:column rendered="#{person.score != null}">
    <f:facet name="header">
      <h:outputText value="The Score" />
    </f:facet>
    #{person.score}
  </h:column> 
</h:dataTable>

The current rendered always makes the column NOT render, even when the scores are not null.

There's no "easy" way to do that, you have to look at the complete collection yourself. You could write helper method that checks your list for null values:

public boolean isAllScoresNull() {
    for(Person p : people) {
        if(p.getScore != null) return false;
    }
    return true;
}

and use that via EL in your page:

...
<h:column rendered="#{myBeanController.allScoresNull}">
...

You could also define a TagFunction for that task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM