简体   繁体   English

启用/禁用dataTable PrimeFaces中的列

[英]Enable/Disable columns in dataTable PrimeFaces

I have created one dataTable in page and shown some columns but I want to show only those columns which have entries ( if any column doesn't have record then it should not be displayed on the page.) . 我在页面中创建了一个dataTable并显示了一些列,但是我只想显示那些具有条目的列(如果任何列没有记录,那么它就不应显示在页面上。)。 Anyone please tell me how should I put validation on this and display table. 任何人都请告诉我我应该如何对此进行验证并显示表格。

You can use the rendered attribute on the column. 您可以在列上使用rendered属性。

For example if your current code is like in the Primefaces showcase (where the entity is Car) you create a method on the bean for every column you would want to omit if all rows are empty. 例如,如果您当前的代码类似于Primefaces展示柜中 (实体是Car的情况),则在bean上为所有行都为空时要忽略的每一列创建一个方法。 For example for the column "Color": 例如,“颜色”列:

public Boolean hasAnyCarColor() {
    for (Car car : cars) {
        if (!(car.getColor() == null) && !(car.getColor().isEmpty()))
            return true;   
    }
    return false;
}

In the view do: 在视图中执行:

<p:column rendered="#{testBean.hasAnyCarColor()}">
    ....
</p:column>

If there are many rows you would probably want to cache the Boolean's in some attributes on the bean. 如果有很多行,您可能希望在Bean的某些属性中缓存布尔值。

I think this code can help you: 我认为这段代码可以帮助您:

<p:column rendered="#{var.login != null}">
 <h:outputText value="#{var.login}" />
</p:column>

the column will be rendered only if the var is not null 仅当var不为null时,才会显示该列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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