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
HTANIRSHTANIRS 

Hide Pageblock Table if there is no values

Hi Friends,

I need help in fixing the VF Pages. 
I created a Class and VF Pages where I want to display a Table values. so, I created a Pageblocktable. Now I have 1 issue when I table has values and other table does not have a values it is diplaying both table which looks awkward.
I need to hide the columns if there is no values.

Below is my VF code and Image for your reference.
<apex:panelGrid columns="3" width="90%">
            <apex:pageblock >
                <apex:pageBlockTable value="{!orderItemsList1}" width="100%" var="div">
                    <apex:column >
                        <apex:pageBlockTable width="100%" columns="1" border="1" cellspacing="0" cellPadding="1" value="{!div.OrderItems}" var="custom">
                            <apex:column headerValue="Body" >
                                <apex:outputField value="{!custom.Product2.Name}"/><p class="onepointfive"/>
                                Blue Plates Required:<apex:outputField value="{!custom.Product2.Blue_plates__c}"/><p class="onepointfive"/>
                                <apex:outputText value="Serial No:" /> <p class="onepointfive"/>
                                <apex:outputText value="Work Order #" />
                            </apex:column>
                        </apex:pageBlockTable>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageblock>
            <apex:pageblock >
                <apex:pageBlockTable value="{!orderItemsList2}" width="100%" var="div">
                    <apex:column >
                        <apex:pageBlockTable width="100%" columns="1" border="1" cellspacing="0" cellPadding="1" value="{!div.OrderItems}" var="custom">
                            <apex:column headerValue="Fridge">
                                <apex:outputField value="{!custom.Product2.Name}"/><p class="onepointfive"/>
                                Blue Plates Required:<apex:outputField value="{!custom.Product2.Blue_plates__c}"/><p class="onepointfive"/>
                                <apex:outputText value="Serial No:" /> <p class="onepointfive"/>
                                <apex:outputText value="Work Order #" />
                            </apex:column>
                        </apex:pageBlockTable>
                   </apex:column>
                </apex:pageBlockTable>
            </apex:pageblock>
            <apex:pageblock >
                <apex:pageBlockTable value="{!orderItemsList3}" width="100%" var="div">
                    <apex:column >
                        <apex:pageBlockTable width="100%" columns="1" border="1" cellspacing="0" cellPadding="1" value="{!div.OrderItems}" var="custom">
                            <apex:column headerValue="Tail Lift" >
                                <apex:outputField value="{!custom.Product2.Name}"/><p class="onepointfive"/>
                                Blue Plates Required:<apex:outputField value="{!custom.Product2.Blue_plates__c}"/><p class="onepointfive"/>
                                <apex:outputText value="Serial No:" /> <p class="onepointfive"/>
                                <apex:outputText value="Work Order #" />
                            </apex:column>
                        </apex:pageBlockTable> 
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageblock>
        </apex:panelGrid> <br/>
Pageblock Table VIew

Kindly advice how can solve this issue.

Thanks. 
Meghna Vijay 7Meghna Vijay 7
Hi,

Use conditional statement to hide/show the pageBlockTable as mentioned below.
<apex:outputpanel rendered = "{!IF(v.orderItemsList1.size >0)}">
<apex:pageBlock>
<apex:pageBlockTable value="{!orderItemsList1}" width="100%" var="div">
</apex:outputPanel>

Hope it helps, if it does mark it as solved.

Thanks
HTANIRSHTANIRS
Hi Meghna,
<apex:outputpanel rendered = "{!IF(v.orderItemsList1.size >0)}">
What is v here. 

am getting Error unknow property v.

Could you please post a snippet of code which works for you.

Thanks,
Meghna Vijay 7Meghna Vijay 7
Hi,

Sorry remove "v." annotation.

Thanks
 
HTANIRSHTANIRS
Hi Meghna,

Getting Error, 
Expected 3 but received 1.

Kindly advise.

Thanks
Meghna Vijay 7Meghna Vijay 7
Hi,

<apex:outputpanel rendered = "{!IF(orderItemsList1.size >0, true, false)}">

I hope now you won't get any error. :)

Thanks
HTANIRSHTANIRS
Hi,
Error got rectified but still I could see the empty pageblock table which does not have values.

Thanks
Meghna Vijay 7Meghna Vijay 7
So you are seeing Body and Fridge even when you applied it. Can you check if you are getting orderItemsList1.size() != 0?

Thanks
Johny SinsJohny Sins
Nice plugin. Thanks for sharing. I am Johny Sins from Plumbers in Texas (https://www.edocr.com/user/cheapplumbersintexas) services.
HTANIRSHTANIRS
Hi Meghna,

Still am seeing Body and Fridge after checking !=0.

Could you please let me know if I need anything to change in class apart from VF.

Thanks.
Ajay K DubediAjay K Dubedi
Hi HTANIRS,
Please try the below code and let me know if this works for you. If still need modifications do let me know.

VFPage:
<apex:page controller="Demo">
    <apex:pageBlock >
        {!con.size }
        <apex:outputPanel rendered="{! IF(con.size > 0, true, false)}">
        <apex:pageBlockTable value="{!con}" var="cn"  >
            <apex:column value="{!cn.Name}"></apex:column>
        </apex:pageBlockTable>
            </apex:outputPanel>
    </apex:pageBlock>
</apex:page>

Controller:
public Demo {
    public List<Contact> con{get;set;}
    public Demo(){
        con = [SELECT Name FROM Contact where Id=: ''];
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi