Skip to main content
I am facing issue when I am trying to move to a different page an at the same time I am fetching the records from the custom object(onclick of a single button) Below are the 2 vf pages with one controller.

Please help me to figure out the problem........................................I will be thankful to the community.

--------------------------------------menu2 vfpage---------------------------------------------

<apex:page controller="menuTypeController" sidebar="false" showChat="false"> <apex:form > <apex:pageBlock > <apex:pageBlockTable value="{!menu}" var="m"> <apex:column value="{!m.Item__c}"/> <apex:column value="{!m.Menu_Description__c}"/> <apex:column value="{!m.Vendor__r.Vendor_Name__c}"/> <apex:column value="{!m.Cost__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form></apex:page>

-----------------------------------menutype vf page---------------------------------------

<apex:page sidebar="false" showChat="false" controller="MenuTypeController">

<apex:pageBlock >

<apex:form >

<apex:commandButton value="INDIAN" action="{!menu2}" reRender="one" onclick="" >

<apex:param name="Indian" value="Indian" assignTo="{!clickedButtonType}"/>

</apex:commandButton>

<apex:commandButton value="CHINESE" alt="CHINESE" action="{!menu2}" reRender="two">

<apex:param name="Chinese" value="Chinese" assignTo="{!clickedButtonType}"/>

</apex:commandButton>

<apex:commandButton value="ITALIAN" alt="ITALIAN" reRender="three" >

<apex:param name="Italian" value="Italian" assignTo="{!clickedButtonType}"/>

</apex:commandButton>

<apex:commandButton value="BRITAIN" alt="BRITAIN" reRender="four">

<apex:param name="Britain" value="Britain" assignTo="{!clickedButtonType}"/>

</apex:commandButton>

<apex:commandButton value="RANDOM" alt="RANDOM" reRender="five">

<apex:param value="Random" name="Random" assignTo="{!clickedButtonType}" />

</apex:commandButton>

</apex:form>

</apex:pageBlock>

-----------------------------------controller for both the classes------------------------------------

public class MenutypeController extends genericControllerClass{

public string clickedButtonType{get;set;}

public list<Menu__c> menuList{get;set;}

public MenutypeController(){

super();

string pageName=getPageName(apexpages.currentPage().getUrl());

if(pageName=='menu2')

{ initmenu2(); }

else if(pageName=='menuType')

{ initmenuType(); }

}

public list<Menu__C> initmenu2()

{ menu2(); return menuList; }

public list<Menu__c> initmenuType(){

return menuList; }

public pageReference menu2(){

menuList=[select cost__c from menu__c where menu_type__c=:clickedButtonType];

pageReference p=new pageReference('/apex/menu2'); p.setRedirect(true);

return p; }

}

 
1 answer
  1. Mar 24, 2021, 12:42 AM
    Hello,

    I am assuming requirment here is to redirect to another page on button click. We have done it using the redirect URL, 'ClikedButtonType' data can be passed as a parmeter in url which should be opened on button click. New page controller should retrieve this url parameter and query the data to display it on opened page. You can use same controller or different one, just make sure to call correct method on new page load.

    I would recommand using output link instead of a CommandButton, it will make it easier if you do not wanna write too much javascript.

    1. Sample output link : <apex:outputlink target="_blank" value="/apex/menu2?btntype=indian" >INDIAN</apex:outputlink>

    2. Retrive Parameter - btntype in controller of menu2 page and display the data. Make sure data loading method is called on action attribute of page. example - loadData() method should do the data query and make it ready for display.

       <apex:page controller="menuTypeController" sidebar="false" showChat="false" action="{!loadData}">

       

    Thanks
0/9000