[SalesForce] How to use 2 pageblockTable values in 1 pageBlocktable

In my code, I have 2 pageblockTable with 2 values(apex).
They are displaying data in 2 different tables. First pageblockTable displays the data based on search button after giving input. Second pageBlockTable displays data when the page loads.
My requirement is to, When I click search button the second pageblockTable old data(default) should disappear and This search data should display in the second pageblockTable.

How to achieve?

Page:

<apex:pageBlock >
    <apex:pageBlockSection >
        <apex:outputLabel ><B>Search</B></apex:outputLabel><BR/>
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Enter Aadhar card Number" for="AdharcardNumberID"/>
            <apex:inputText value="{!adharcard}" id="AdharcardNumberId"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Last name" for="lastnameId"/>
            <apex:inputText value="{!lastname}" id="lastnameId"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >

            <apex:outputLabel value="Surname" for="SurnameId"/>
            <apex:inputText value="{!surname}" id="SurnameId"/>
        </apex:pageBlockSectionItem>

        <apex:commandButton value="Search" action="{!searchRec}" reRender="recMatch, recNotMatch"/>

        <apex:outputPanel id="recMatch">
            <apex:pageblockSectionItem rendered="{!Match}">
                <apex:pageblockTable value="{!searchObj}" var="obj">
                    <apex:column value="{!obj.field1}"/>
                    <apex:column value="{!obj.field2}"/>
                    <apex:column value="{!obj.field3}"/>
                    <apex:column value="{!obj.field4}"/>
                    <apex:column value="{!obj.field5}"/>
                    <apex:column value="{!obj.field6}"/>
                    <apex:column value="{!obj.field7}"/>
                    <apex:column value="{!obj.field8}"/>
                </apex:pageblockTable>
            </apex:pageblockSectionItem>

        </apex:outputPanel>

        <apex:outputPanel id="recNotMatch">
            <apex:pageBlockSectionItem rendered="{!NoMatch}"> Record Not Found!</apex:pageBlockSectionItem>
        </apex:outputPanel>
    </apex:pageBlockSection>

</apex:pageBlock>    


<!--second PageblockTable -->
<apex:pageBlock id="tablePB">
    <apex:pageBlockSection title="Obj List">
        <apex:pageBlockTable value="{!Objects}" var="ob">
            <apex:column headerValue="Last Name">
                <apex:outputLink value="/{!vict.id}" target="_blank"><apex:outputText value="{!ob.Name}" /></apex:outputLink>
            </apex:column>
            <apex:column value="{!ob.field1}"/>

            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>
            <apex:column value="{!ob.field1}"/>


        </apex:pageBlockTable><BR/>

Best Answer

Here the VF page is having 3 variables that is controlling the visibility of the tables/sections: "NoMatch", "Match" and "SerchMatch". Match is used to show the table which you want to displace without search, NoMatch is used to displace record not found text msg section and SerchMatch is used to displace your search result. One you hit the search button your search method is called and performs the search. Here we are controlling the visibility controlling variables.

Please use the below VF page :

<apex:page controller="ShowPageBlock_Class">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Last name" for="lastnameId"/>
                <apex:inputText value="{!lastname}" id="lastnameId"/>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Surname" for="SurnameId"/>
                <apex:inputText value="{!surname}" id="SurnameId"/>
            </apex:pageBlockSectionItem>

            <apex:commandButton value="Search" action="{!searchRec}" reRender="recMatch, recNotMatch,SerachRcdId,recMatchMrgd"/>

        </apex:pageBlockSection> 
    </apex:pageBlock>   

    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputPanel id="recNotMatch">
                <apex:pageBlockSectionItem rendered="{!NoMatch}"> Record Not Found!</apex:pageBlockSectionItem>
            </apex:outputPanel> 

        </apex:pageBlockSection>

        <apex:pageBlockSection >                
            <apex:outputPanel id="SerachRcdId">
                <apex:pageblockSectionItem rendered="{!SerchMatch}">
                    <apex:outputText >Searched Result</apex:outputText>
                    <apex:pageblockTable value="{!searchAccontLst}" var="a">
                         <apex:column headervalue="Account Name" value="{!a.Name}"/>
                         <apex:column headervalue="Account Type" value="{!a.type}"/>   
                    </apex:pageblockTable>
                </apex:pageblockSectionItem>    
            </apex:outputPanel>
        </apex:pageBlockSection> 

        <apex:pageBlockSection >
            <apex:outputPanel id="recMatch">
                <apex:pageblockSectionItem rendered="{!Match}">
                    <apex:outputText >Fixed Table</apex:outputText>
                    <apex:pageblockTable value="{!searchObj}" var="a">
                         <apex:column headervalue="Account Name" value="{!a.Name}"/>
                         <apex:column headervalue="Account Type" value="{!a.type}"/>
                         <apex:column headervalue="Industry" value="{!a.industry}"/>    
                    </apex:pageblockTable>
                </apex:pageblockSectionItem>

            </apex:outputPanel>
        </apex:pageBlockSection>
    </apex:pageBlock>       
</apex:form>

At controller use this:

public with sharing class ShowPageBlock_Class {
public String lastname{get;set;}
public String surname{get;set;}
public Boolean NoMatch{get;set;}
public Boolean Match{get;set;}
public List<Account> searchObj{get;set;}
public List<Account> searchAccontLst{get;set;}
public Boolean SerchMatch{get;set;}    


public ShowPageBlock_Class(){
    NoMatch = False;
    Match = True;  
    SerchMatch = False;
    searchObj = new List<Account>();
    searchObj = [select id,name,type,industry from Account];
}

public Void searchRec(){         
    searchAccontLst = new List<Account>();
    searchAccontLst = [select id,name,type,industry from Account where name=:lastname];
    if(searchAccontLst.size()==0){
        NoMatch = True;
        SerchMatch = False; 
        Match = False;          
    }
    else if(searchAccontLst.size()>0){
        NoMatch = False;
        SerchMatch = True;
        Match = False;
    }

}

}

Snap : Default screen before serach :enter image description here

Snap: After search : When result found: enter image description here

Snap: When record not found enter image description here

Related Topic