You need to sign in to do that
Don't have an account?
Tyler Harris
Grab Output Text ID in Visualforce, Compare it to Map in Apex
I'm trying to grab the ID from the Visualforce page, pass it into a list, and if ID is in the list remove it from my cart. I'm getting a "Incompatible key type List<Id> for Map<Id,StoreFront2.DisplayMerchandise>" on line 26.
Visualforce
Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2"> <apex:stylesheet value="{!URLFOR($Resource.styles)}"/> <h1>Store Front</h1> <apex:variable var="rowDel" value='{!0}'/> <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:param name='rowDel' 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:column> </apex:dataTable> </apex:form> </apex:page>Apex
public class StoreFront2 { public String message { get; set; } List<DisplayMerchandise> products; Map<Id, DisplayMerchandise> cart; public List<Id> row2Del{get;set;} private 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 = ApexPages.currentPage().getParameters().get(rowDel); row2Del.add(rowz); if(cart.containsKey(row2Del)){ cart.remove(row2Del); } } 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 Integer rowNumber { 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; } }
All Answers
Visualforce
Apex