簡體   English   中英

根據JSF2中的條件隱藏數據表的行

[英]Hide rows of datatable based on condition in JSF2

我在 JSF2 中使用 MyFaces <h:datatable> ,我想根據如下條件隱藏行。

但是我的條件好像不行。

<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>

我也嘗試只使用 rowStyleClass 但它也不起作用

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

有什么建議么 ?

答案很簡單。 h:dataTable中沒有rowStyleClass

只有一個rowClasses屬性( docs )。

您可以在PrimeFaces 的數據表中找到rowStyleClass

這有點棘手,因為在 rowClasses 中您不能編寫條件,因此解決方案是在 Bean 中創建一個函數,該函數返回逗號分隔的 css 類列表並在 xhtml 中調用它

在 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}" ...>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM