Tuesday, 15 December 2015

How to check list size in visualforce page

Most of peoples wanted to display the table if the list has records. For this you can use the below approach,

Apex Class:

  1. public class AccountController
  2. {
  3.     public List<Account> accList{get; set;}
  4.     public AccountController()
  5.     {
  6.         accList = [SELECT Id, Name FROM Account LIMIT 10];
  7.     }
  8. }

Visualforce Page:

  1. <apex:page controller="AccountController">
  2.     <apex:pageBlock title="Account List" >
  3.         <apex:outputPanel rendered="{! IF(ISBLANK(accList), false, true) }">
  4.             <apex:pageBlockTable value="{!accList}" var="acc" rendered="{!accList.size > 0}">
  5.                 <apex:column headerValue="Account Name"> {!acc.Name} </apex:column>
  6.             </apex:pageBlockTable>
  7.         </apex:outputPanel>
  8.     </apex:pageBlock>
  9. </apex:page>

Two conditions are checked in the above visualforce page,

Condition 1:

{!IF(ISBLANK(accList), false, true)}

This will ensure to prevent from displaying table in the following conditions,
1. LIST not yet initialized. 
2. LIST has been initialized, but no records.

Condition 2:

{!accList.size > 0}

Table will be displayed only if the LIST contains at least one record.

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...