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
Shephali SwarnkarShephali Swarnkar 

How to show only one table a time based link clicked

Hi All,
     Below is my code which shows the table contents based on link clicked.But here it shows both the table. My requirement is to show only one table at a time and hide another one and also initially the pageblock shows the blank structure of the pageblocktable initially.Is there any way to avoid such things too???

<apex:page controller="RerenderController">
 <apex:form >
  <apex:pageBlock >
<apex:pageBlockTable value="{!num_con}" var="cn" id="p1" >
<apex:column value="{!cn.Name}" />
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!num_con}" var="cn" id="p2">
<apex:column value="{!cn.Name}"/>
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
<apex:column value="{!cn.phone}"/>
<apex:column value="{!cn.Contact_Type__c}"/>
</apex:pageBlockTable>
<apex:commandLink action="{!created_con}" value="table1"  reRender="p1"/>
<apex:commandLink action="{!createdpros}" value="table2" reRender="p2"/>
</apex:pageblock>
</apex:form>
</apex:page>
==========================Controller=====================
public class RerenderController
{
    public List<contact> num_con {get;set;}      
    public void created_con()
    {  
     num_con = [select name, account.name, mobilephone, phone, Contact_Type__c, Email from contact];
     }
      public void createdpros()
     {
     num_con = [select Name, Email, MobilePhone, Account.Name, Phone, Contact_Type__c from Contact where Contact_Type__c='prospect' and owner.alias='myada'];     
     }
   
}
Best Answer chosen by Shephali Swarnkar
Abhishek BansalAbhishek Bansal
Hi Shephali,

Please change your Vf page and controller class code as below :

VF Page :
<apex:page controller="RerenderController">
 <apex:form >
  <apex:pageBlock id="pb">
<apex:pageBlockTable value="{!num_con}" var="cn" id="p1" rendered="{!showCons}">
<apex:column value="{!cn.Name}" />
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!num_con}" var="cn" id="p2" rendered="{!showPros}">
<apex:column value="{!cn.Name}"/>
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
<apex:column value="{!cn.phone}"/>
<apex:column value="{!cn.Contact_Type__c}"/>
</apex:pageBlockTable>
<apex:commandLink action="{!created_con}" value="table1"  reRender="pb"/>
<apex:commandLink action="{!createdpros}" value="table2" reRender="pb"/>
</apex:pageblock>
</apex:form>
</apex:page>

Controller Class :
public class RerenderController
{
    public List<contact> num_con {get;set;}  
	public boolean showCons{get;set;}
	public boolean showPros{get;set;}
	public RerenderController(){
		showCons = false;
		showPros = false;
	}
    public void created_con()
    {  
	showCons = true;
	showPros = false;
     num_con = [select name, account.name, mobilephone, phone, Contact_Type__c, Email from contact];
     }
      public void createdpros()
     {
	 showCons = false;
	showPros = true;
     num_con = [select Name, Email, MobilePhone, Account.Name, Phone, Contact_Type__c from Contact where Contact_Type__c='prospect' and owner.alias='myada'];     
     }
   
}

Let me know if you have any issue.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi Shephali,

Please change your Vf page and controller class code as below :

VF Page :
<apex:page controller="RerenderController">
 <apex:form >
  <apex:pageBlock id="pb">
<apex:pageBlockTable value="{!num_con}" var="cn" id="p1" rendered="{!showCons}">
<apex:column value="{!cn.Name}" />
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!num_con}" var="cn" id="p2" rendered="{!showPros}">
<apex:column value="{!cn.Name}"/>
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
<apex:column value="{!cn.phone}"/>
<apex:column value="{!cn.Contact_Type__c}"/>
</apex:pageBlockTable>
<apex:commandLink action="{!created_con}" value="table1"  reRender="pb"/>
<apex:commandLink action="{!createdpros}" value="table2" reRender="pb"/>
</apex:pageblock>
</apex:form>
</apex:page>

Controller Class :
public class RerenderController
{
    public List<contact> num_con {get;set;}  
	public boolean showCons{get;set;}
	public boolean showPros{get;set;}
	public RerenderController(){
		showCons = false;
		showPros = false;
	}
    public void created_con()
    {  
	showCons = true;
	showPros = false;
     num_con = [select name, account.name, mobilephone, phone, Contact_Type__c, Email from contact];
     }
      public void createdpros()
     {
	 showCons = false;
	showPros = true;
     num_con = [select Name, Email, MobilePhone, Account.Name, Phone, Contact_Type__c from Contact where Contact_Type__c='prospect' and owner.alias='myada'];     
     }
   
}

Let me know if you have any issue.

Thanks,
Abhishek
This was selected as the best answer
Shephali SwarnkarShephali Swarnkar
Hi Abhishek,
       Thanks a lot...!!!!
        Its working well but can we do the same thing using any standard variable because i have 8 such table so having variable for each and defining true and false is somehow wil increase the lines of code. 
Abhishek BansalAbhishek Bansal
Hi Shephali,

You cannot control the visibility of multiple table with a single boolean variable.
But you can achieve this with the help of a String Variable like below :

public String showTable {get;set;}

In Constructor initialize this variable as :
showTable = 'Hide All'
    
In each of your methods change this String to :
showTable = 'Show Pros Table';
Or
showTable = 'Show Cons Table';

In VF page change the rendered condition as below :

For table 1 rendered="{!If(showTable == 'Show Cons Table',true,false)}"
For table 2 rendered="{!If(showTable == 'Show Pros Table',true,false)}"

This is the only way to handle visibility with single varibale.

Let me know if you need more help.

Thanks,
Abhishek 
Shephali SwarnkarShephali Swarnkar
Thanks Abhishek..
         Thats working too.
Shephali SwarnkarShephali Swarnkar
Hi Abhishek,
    this is my code:
              <apex:pageBlock id="pb1">
               <apex:pageBlockTable value="{!num_con}" var="cn" rendered="{!If(showTable == 'contacts',true,false)}">
<apex:column headerValue="Contact Name">
<apex:commandLink value="{!cn.Name}" reRender="block1">
<apex:param name="p1" value="{!cn.ID}"/>
 </apex:commandLink> 
 </apex:column>
<apex:column value="{!cn.Account.Name}"/>
<apex:column value="{!cn.Email}"/>
<apex:column value="{!cn.mobilephone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
    <apex:pageBlock id="block1">
      <apex:detail subject="{!$CurrentPage.parameters.p1}" relatedList="false"/>
    </apex:pageBlock>

The above code is not working properly, as showing detail record for one click but when clicking second time on any of the link it doesnot show any data.but when i remove the attribute-> rendered="{!If(showTable == 'contacts',true,false)}"  It works but also showing the table structure initially which i need to avoid.

i tried    rendered="{!(num_con.size >0)}"
throwing error: The value 'null' is not valid for operator '>'
Error is in expression '{!(num_con.size >0)}' in component <apex:pageBlockTable> in page.....


how to achieve this...??? that not showing the table structure intially but and showing the detail records for each link.

can you suggest me to resolve this???

 
Abhishek BansalAbhishek Bansal
Hi Shephali,

It would be easy to help you if you can contact me on either gmail or skype :
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790

Thanks,
Abhishek