In a custom Visualforce page with a standard controller, how can I check and conditionally render related lists based on availability of records in the related list?
|
|
Ideally one would like to write a rendered expression like so {!IF(Text__c.Test_Children_r.size > 0, TRUE, FALSE)}. However this results in an error as Visualforce is expecting a field name from the child object. Shame. So failing this, you then have two options, the first of which is preferred, as the second option, while it works is strictly speaking unsupported according to the docs. Apex extension controller option This example uses a controller property to expose the count. It also avoids having to perform the SOQL query itself by letting the standard controller know in advance that this information is required (typically driven by field references in the page).
You can then refer to the count in the rendered attribute as follows.
Visualforce only option NOTE: This option does work, but is technically unsupported, see the warnings about reassigning apex:variables within apex:repeat here.
|
|||||||||||||||
|
|
This question prompted me to do some more research on this, and I came up with an alternative, though still unsupported, VisualForce only solution:
It's a bit more concise than the apex:variable method, and doesn't involve changing the values assigned to a variable. That said, it's relying on an empty array always being represented as '[]', though I think we're on relatively safe ground with that assumption. |
||||
|
|