You need to sign in to do that
Don't have an account?
Tyler Harris
Conditionally display commandButton based on dataTable variable?
Hello, I want to conditionally display a "Checkout" button if a merchandise item is added to the cart in the visualforce dataTable. How can I accomplish this in visualforce?
Apex
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!carti} <>null"/> </apex:form>
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2"> <apex:stylesheet value="{!URLFOR($Resource.styles)}"/> <h1>Store Front</h1> <apex:form > <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even"> <apex:column headerValue="Product"> <apex:outputText value="{!pitem.merchandise.name}" /> </apex:column> <apex:column headervalue="Price"> <apex:outputText value="{!pitem.merchandise.Price__c}" /> </apex:column> <apex:column headerValue="#Items"> <apex:inputText value="{!pitem.tempCount}"/> </apex:column> </apex:dataTable> <br/> <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart"> </apex:commandButton> </apex:form> <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/> <h1>Your Basket</h1> <apex:form > <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even"> <apex:column headerValue="ID" rendered="false"> <apex:outputText value="{!cart[carti].merchandise.Id}" > </apex:outputText> </apex:column> <apex:column headerValue="Product"> <apex:outputText value="{!cart[carti].merchandise.name}"> </apex:outputText> </apex:column> <apex:column headervalue="Price"> <apex:outputText value="{!cart[carti].merchandise.price__c}" /> </apex:column> <apex:column headerValue="#Items"> <apex:outputText value="{!cart[carti].count}"/> </apex:column> <apex:column headerValue="Remove?"> <apex:commandButton action="{!Remove}" value="Remove" reRender="cart"> <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/> </apex:commandButton> </apex:column> </apex:dataTable> <apex:commandButton value="Checkout" action="{!checkout}" rendered="{!rowDel}"/> </apex:form> </apex:page>
Apex
public virtual class StoreFront2 { public String message { get; set; } List<DisplayMerchandise> products; Map<Id, DisplayMerchandise> cart; public Id rowz; public id rowDel{get;set;} public PageReference shop(){ handleTheBasket(); message = 'You bought: '; for (DisplayMerchandise p:products){ if(p.tempCount > 0){ message += p.merchandise.name + ' (' + p.tempCount + ') ' ; } } return null; } public void remove(){ rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel'); system.debug(rowz); if(cart.containsKey(rowz)){ cart.remove(rowz); } } public pageReference back(){ PageReference doit = new PageReference('/apex/StoreCart'); doit.setRedirect(false); return doit; } public Pagereference checkout(){ PageReference send = new PageReference('/apex/ConfirmBuy'); //send.setRedirect(false); return send; } public void handleTheBasket(){ for(DisplayMerchandise c : products){ if(c.tempCount > 0){ if(cart.containsKey(c.merchandise.Id)){ cart.get(c.merchandise.Id).count += c.tempCount; } else{ cart.put(c.merchandise.Id, c); cart.get(c.merchandise.Id).count = c.tempCount; } } } } public Map<Id, DisplayMerchandise> getCart() { if(cart == null){ cart = new Map<Id, DisplayMerchandise>(); } return cart; } public class DisplayMerchandise { public Merchandise__c merchandise{get; set;} public Decimal count{get; set;} public Decimal tempCount{get;set;} public DisplayMerchandise(Merchandise__c item){ this.merchandise = item; } } public List<DisplayMerchandise> getProducts() { if (products == null){ products = new List<DisplayMerchandise>(); for (Merchandise__c item : [SELECT id, name, description__c, price__c FROM Merchandise__c WHERE Total_Inventory__c > 0]) { products.add(new DisplayMerchandise(item)); } } return products; } }
The problem is in your below method
Line 5 It should be inside if method
The updated method should be Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
All Answers
Use this :
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{! cart.size > 0 }" />
Hi Tyler Harris,
The code given by ajay should work for you.The only thing you need to check is for Null check
If you have a requirement where you are adding and removing the cart items then you need to follow below approach.
You need to Wrap your button inside a panel and add an Id to it say id="panelid"and use the rendered attribute on "cart.size".
Another important thing you need to do is to have a reRender function on the action where you are updating the cart(adding or deleting the card records)
say you have a buttons in that use the reRender attribute as reRender"panelid" and you are done !!!!!!!!!!!!!!!!!!!!!!
Let me in case of any issue
Never use multiple apex:form, It degrades the performance of the page while increasing view state.
do one thing what ever the products getting to datalist right in each product row we can create button and add into cartpage,once we saw flipkart we can get a idea about this issue
I've got a property
How can I pass it to the rendered attribute?
I've added the logic in my controller, but it's not rendering on the page.
Apex
Give little more description what are you expecting and what it is actually showing.
Seems like you are not rerendering the command button on click of Add to Cart!
Some suggestions for having nice standard look and feel in your page, if you would like:When you paste the code, make sure the intendation is proper. It make your code readable easily :)
Changed the intendation properly, and removed the additional unneeded apex:form. Now its is rerendering the whole cartPanel on click of Add to Cart
1. You can use apex:pageblock and apex:pageBlocksection instead of h1 tag.
2. For apex:commandButton you can enclose inside apex:pageBlockButtons.
3. Could use apex:pageBlockTable instead of apex:dataTable
Hopt it helps :)
This isn't working and now it's not adding to the cart.
It is because below command button has <b> after msg, so I will remove it and you can try.
Try the above code and let us know.
11:13:23:049 USER_DEBUG [78]|DEBUG|true
The reason behind this is that incart is false in the page load
You need to wrap the button inside a panel and reRender it
Try following
If you have not tried to add the products try and let us know
Apex:
Visualforce
The problem is in your below method
Line 5 It should be inside if method
The updated method should be Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.