user1745306
user1745306

Reputation:

Data Table header visible during page load even if there is no data to display

I'm working on an application with Spring, JSF, Hibernate following the example in this link.

When I run this code with some values populated in the database, this should have the datatable header name along with all the rows retrieved. But when there is no value in the database and when I run the code, this shouldn't display the datatable header name. The datatable header name should only be displayed only when there is some values retrieved from the database and not during startup. How do I do this?

Upvotes: 0

Views: 1283

Answers (2)

BalusC
BalusC

Reputation: 1108782

Just let the rendered attribute of the <h:dataTable> check if the data model is not empty.

<h:dataTable value="#{customer.customerList}" ... rendered="#{not empty customer.customerList}">

No need to clutter the model with an irrelevant property, like as suggested by the other answer.

See also:

Upvotes: 1

erencan
erencan

Reputation: 3763

Add a integer property to your managed bean which gets size of list

private int sizeOfTable;

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

You can control the data table by rendered attribute.

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

If the size of the data table is 0 then the data table will not be rendered. If you want to hide the header only you can add the rendered value to h:column

However, Database rows are retreived for a single count operation. It is not a good practice. You should handle database accesses according to your needs. A database access is a IO operation for a computer which is the most costy part of operations.

My suggestion is to store the list in a data property and work on it.

private List<Customer> customerList;

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


            return customerList;
        }

and you should edit your size property accordingly.

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

Upvotes: 1

Related Questions