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
TehNrdTehNrd 

How to render a table only if it has values?

I have a page that dynamically returns results to several different tables but I only want these tables to be rendered if they contain values. I would like to put this logic in the page if possible as there are 10 tables and I'd rather not build out 10 methods in the controller if I can do this one a simple line of code in the page.

Here is what I have tried but with no luck. With this code the table is never rendered and I'm not even sure if I can user the .size() method here....

Code:
<apex:pageBlockTable value="{!results}" var="r" rendered="{IF(!results.size() > 0,true,false)}">

Thanks,
Jason


Message Edited by TehNrd on 08-26-2008 10:25 AM
jonathanrico.jonathanrico.
Have you tried:


Code:
<apex:pageBlockTable value="{!results}" var="r" rendered="{!results != null}">

 
Not sure if this works..
lnryanlnryan
try removing the redundant IF clause...ie

change: IF(!results.size() > 0,true,false)
to: !results.size()>0

expressions evaluate to booleans first anyway.

TehNrdTehNrd
This always shows the table. I believe this is because the array is never null, just empty. If it was null I would get null exception errors:

rendered="{!results != null}"

And this never shows the table, similar to my original code:

rendered="!results.size()>0"
TehNrdTehNrd
Just tried this with no luck:

rendered="IF(!results.isempty(),false,true}"
lnryanlnryan
have you debuged your results.size() call by displaying the actual size value anywhere in your page?
TehNrdTehNrd
You know what, I did try that but received an error message when trying to save the page:
Code:
<apex:outputText value="{!results.size()}"/>
 
Error: Unknown function results.size. Check spelling.

Strange that it saves fine when in the rendered attribute but not the value attribute.



Message Edited by TehNrd on 08-26-2008 09:46 AM
lnryanlnryan
try
!results.size

it may be that size is a property rather than a method. i vaguely recall running into similar difficulties once before.


Message Edited by lnryan on 08-26-2008 09:48 AM
TehNrdTehNrd
I get this error when trying to save "!results.size"

Error: The class 'java.util.ArrayList' does not have the property 'size'.
lnryanlnryan
can you provide more information about the underlying apex class - or just paste the relevant code. Specifically, how is results set up. Is it a List object, if so, is it typed has a generic sObject, a specific object, a mapping of Objects to IDs?



jonathanrico.jonathanrico.
There seems to be no way to access the ArrayList properties in the rendered attribute.. I can't think of any other way but creating a function that returns a boolean value indicating if the ArrayList is empty.. Not very elegant though..
dchasmandchasman
Currently List.size() and Array.size() etc are not exposed via getters that conform to the requirements of VF expressions. Fixing this is on my teams backlog but this is not going to be included in the Winter '09 release. You can work around this by exposing a property that provides access to this information to VF expressions yourself on your custom controller or extension.
TehNrdTehNrd
Thanks for clarify Doug. I'm not sure how much work it would but be perhaps there could be an error when trying to save with this in the rendered attribute.

I guess my controller is just going to have ten of these :( .....

Code:
public boolean getShowTable1(){
   boolean show = false;
   if(results.size() > 0){
      show = true;
   }
   return show;
}

 





Message Edited by TehNrd on 08-26-2008 10:35 AM
ptepperptepper
Have you tried writing a method in your Apex that returns the size, instead of trying to get that number it in the Visualforce logic?

That is, assuming you're getting your data from a query, make a variable, e.g. 'myResult' to hold the results of the query. The variable would be a list of whatever objects your query returns. Then make a getter method that returns myList.size() -- an int.

-paul




Message Edited by ptepper on 08-26-2008 10:46 AM
TehNrdTehNrd
That's definitely an option but I'd rather not split up my logic between the controller and the page. I would have a method that calculates size and then I would have to evaluate that size in in the page. I will keep all of this logic in the controller just to keep it simpler.
visulaforcevisulaforce

u can use smthing like this :

<apex:pageblockTable id="docTable" value="{!partialList}" rendered="{!NOT(ISNULL(partialList))}" var="docProperty" rows="{!rowCount}" columns="10">

 

SoloSolo

Hello, I 've got the same needs but looks live Visualforce pages does not support size() function

 

So I did extra cycle before table output to clarify if the object have values for table' rows.

If I have more than zero objects I start table - otherwise skip it

Here  is a code:

<apex:variable value="{!0}" var="rowNum"/>
    <apex:repeat var="count" value="{!Property__c.Additional_Agreement_Financials__r}">
    <apex:variable var="rowNum" value="{!rowNum+1}"/>
</apex:repeat>
<apex:outputPanel id="thePanel" layout="none" rendered="{!rowNum>0}">

  <!-- Table here -->  

</apex:outputPanel>