0

Hello, everyone! I am developing a website on Salesforce. And I have some pages redirections along with usage of rendered. In particular, I have a home page where products divided by categories are hidden in blocks, which are rerendered after we choose category reference. And I have some page which lists all products - Products page. It should by click on the category (it is sidebar) redirect to a home page and rerender block for the specific category. Is that possible to use page reference and rendered simultaneously? By the way, I have custom Product object with fields like Name, Description__c, Price__c, Quantity__c and Category__c (TV, Laptop. Mobile phone, Monitor). Your help will be appreciated!!

Here is a code snippet from home page:

  <div class="main">
    <apex:form id="monitorsForm" rendered="{!renderMonitor}">
        <apex:outputPanel id="monitorsDetail">
            <apex:pageBlock title="Monitors" id="monitorsBlock">
                <br/>
                <apex:pageBlockTable value="{!monitorsList}" var="monitor">
                    <apex:column >
                        <apex:commandLink value="{!monitor.Name}" >
                            <apex:param name="id" value="{!monitor.Id}"/>
                        </apex:commandLink>
                        <apex:pageBlockSection >
                            <apex:image value="{!URLFOR($Resource.Monitor, monitorsImages[monitor.Name])}" width="600" height="400" id="monitorImage"/>
                        </apex:pageBlockSection>
                        <apex:pageBlockSection >
                            <apex:outputText value="{!monitor.Price__c}" id="monitorPrice"/><br/>
                            <apex:outputText value="{!monitor.Description__c}"/>
                        </apex:pageBlockSection>
                        <br/>
                        <apex:commandButton value="Add to cart" action="{!addProductToShoppingCart}" reRender="cartFrom">
                            <apex:param name="itemsId" value="{!monitor.Id}" assignTo="{!chosenItemId}"></apex:param>
                        </apex:commandButton>
                        <br/><br/>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:pageBlockSection >
                    <apex:outputText value="There are {!monitorsQuantity} products in this category" id="productsNumber"/><br/>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</div>

This is sidebar for categories in Products page:

   <div class="sidebar">
    <apex:form id="categories">
        <apex:pageblock title="Categories" id="blockA">
            <apex:commandLink action="{!redirectLaptopsFromHome}" id="theLink">Laptops</apex:commandLink>
            <br/>
            <apex:commandLink action="{!redirectMonitorsFromHome}" id="theLink2">Monitors</apex:commandLink>
            <br/>
            <apex:commandLink action="{!redirectTVsFromHome}" id="theLink3">TVs</apex:commandLink>
            <br/>
            <apex:commandLink action="{!redirectMobilesFromHome}" id="theLink4">Mobile phones</apex:commandLink>
        </apex:pageblock>
    </apex:form>
</div>

And some code from controller for redirection to some category:

public PageReference redirectProductPage(){
    if (currentPage == null) {
        PageReference pageRef = new PageReference('/apex/ProductsPage');
        pageRef.setRedirect(true);
        return pageRef;
    } else {
        return null;
    }
}

public PageReference redirectMonitorsFromHome() {
    redirectProductPage();
    renderMonitor = true;
    renderTV = false;
    renderLaptop = false;
    renderMobile = false;
    renderShoppingCart = false;
    return null;
}
  • Have you tried reRendering monitorsForm on your <apex:commandButton>? Until you re-render that panel, I'd expect the rendered state of monitorsForm to always be set to the value it was initialised with. – Dan Jones Mar 8 '17 at 17:20
  • @DanJones which button exactly do you mean? – Cassie Mar 8 '17 at 17:26
  • Scratch that. On your <apex:commandLink action="{!redirectMonitorsFromHome}, you're not reRendering monitorsForm, or anything for that matter, so it's maintaining whatever it was set to beforehand. If the sidebar is a Visualforce Component and was literally taking you from Page A to Page B as implied in your question, you'd probably need to set a parameter in the URL and check what should be displayed based on that. company.my.salesforce.com/apex/myPage?foo=bar&hello=world, something like that. – Dan Jones Mar 8 '17 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.