function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adamproadampro 

Display No Results in PageBlockTable

Hi,

 

I'm making a pageBlockTable to display a list of custom objects. However, there are times where there won't be any objects to display. Is there any way I can make it show that there are no objects returned in the query, rather than it just being a blank table?

 

I tried this to populate the list with an object that would display 'No records to display', but it returned an error saying : "Error: PartExtension Compile Error: Variable does not exist: Name at line 29 column 52" 

 

(line 29 is the line displayed below)

 

else{
                objectsList.add( new Custom_Object__c (Name = 'No records to display'));
            }

 

What other options do I have?

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hello,

Just want to extend the Starz26 idea.

 

if suppose in Controller you have list name contactList,

then you can check directly if the size of the list is 0 or not.

<apex:pageBlockTable value="{!innerContactList}" var="innerCont" rendered="{!(contactList.size != 0)}">

 hope it helps :)

All Answers

Starz26Starz26

You can use the rendered attribute to hide the pageblock table if no records are returned and create a message that displays in its place using the rendered attribute if no records are returned....

 

 

adamproadampro

I've done a little bit of rerender work, but I'm not sure how to do this exactly. How can I rerender if the controlling list has a size of 0?

 

Sorry, I'm pretty new to Visualforce & web design.

Starz26Starz26

In the Controller or extension

 

         Public Integer objSize {get;set;}

 

When you do the SOQL to return the data and assign to a list or whatever, add

 

         objSize = LISTVARIABLE.size(); **This should be what your Pageblock table uses as the Value

 

Then in your pageblock table add

 

        rendered="{!IF(objSize>0,TRUE,FALSE)}"

 

 

Rahul SharmaRahul Sharma

Hello,

Just want to extend the Starz26 idea.

 

if suppose in Controller you have list name contactList,

then you can check directly if the size of the list is 0 or not.

<apex:pageBlockTable value="{!innerContactList}" var="innerCont" rendered="{!(contactList.size != 0)}">

 hope it helps :)

This was selected as the best answer
craigmhcraigmh

why not just use innerContactList.size?

 

I normally do something like this:

 

 

<apex:pageBlockTable value="{!innerContactList}" var="innerCont" rendered="{!(innerContactList.size != 0)}">
   (pageBlockTable stuff)
</apex:pageBlockTable>
<apex:outputText rendered="{!(innerContactList.size = 0)}" value="There are no contacts to display." />