简体   繁体   中英

Hide rows of datatable based on condition in JSF2

I'm using MyFaces <h:datatable> in JSF2 and I would like to hide rows based on condition like below.

But it seems that my condition doesn't work.

<h:dataTable value="#{myController.persons}" var="person"
    styleClass="table table-striped table-lg borderless"
    headerClass="table-header borderless header-cell"
    rowClasses="table-show-row,table-hide-row"
    rowStyleClass="#{person.gender ne 'M'}">
    <h:column>
        <div>
            <div class="">
                <f:facet name="header">Name</f:facet>
            </div>
            <div class="">
                #{persone.name}
            </div>
        </div>
    </h:column>
</h:dataTable>

I also tried to use only rowStyleClass but it doesn't work also

rowStyleClass="#{person.gender ne 'M' ? 'table-show-row': 'table-hide-row'}">

any suggestions ?

The answer is pretty simple. There's no rowStyleClass in h:dataTable .

There is only a rowClasses attribute ( docs ).

You can find rowStyleClass in data tables from PrimeFaces .

it was a bit tricky because in rowClasses you can't write a condition so the solution is to create in Bean a function which returns list of comma separated css classes and call it in xhtml

In Bean:

    String rowStyles;

    public String getRowStyles() {
        rowStyles = "";
        for (Person person: persons) {
            rowStyles += person.getGender().equals('M') ? "table-hide-row," : "table-show-row,";
        }

        return rowStyles;
    }

XHTML:

<h:dataTable rowClasses="#{personBean.rowStyles}" ...>

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