即使没有要显示的数据,在页面加载期间数据表标题也可见

时间:2013-05-10 05:00:49

标签: jsf datatable

我按照this link中的示例使用Spring,JSF,Hibernate处理应用程序。

当我运行此代码时,数据库中填充了一些值,这应该具有数据表头名称以及检索到的所有行。但是当数据库中没有值并且运行代码时,这不应该显示数据表头名称。只有在从数据库中检索到某些值而不是在启动期间检索时,才应显示数据表头名称。我该怎么做?

2 个答案:

答案 0 :(得分:1)

向托管bean添加integer属性,其大小为列表

private int sizeOfTable;

public int getSizeOfTable()
{
return customerBo.findAllCustomer().size();
}
public int setSizeOfTable(int sizeOfTable(int sizeOfTable)
{
this.sizeOfTable = sizeOfTable;
}

您可以按rendered属性控制数据表。

<h:dataTable value="#{customer.getCustomerList()}" var="c" rendered="#{customer.sizeOfTable == 0}"
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row"
            >

如果数据表的大小为0,则不会呈现数据表。如果您只想隐藏标题,可以将rendered值添加到h:column

但是,对于单个计数操作,将检索数据库行。这不是一个好习惯。您应该根据需要处理数据库访问。数据库访问是计算机的IO操作,这是操作中最耗费的部分。

我的建议是将列表存储在数据属性中并对其进行处理。

private List<Customer> customerList;

    public List<Customer> getCustomerList(){
    if(customerList == null)
    {
    customerList =  customerBo.findAllCustomer();
    }


            return customerList;
        }

你应该相应地编辑你的尺寸属性。

  public int getSizeOfTable()
    {
    return customerList.size();
    }

答案 1 :(得分:1)

如果数据模型不是rendered,只需让<h:dataTable>的{​​{1}}属性检查。

empty

不需要使用不相关的属性来混淆模型,就像其他答案所建议的那样。

另见: