I have some sort of shop. And the checkbox near the names of the items. How to find out, which checkboxes was selected? And then, add the names of items which ckechboxes was selected to the shopping cart? Now the programm only can add items to the shopping cart, when all checkboxes are selected. I tried to select them by lists. But it doesn't work.
My Visualforce Page.
<apex:page controller="StoreFrontController">
<apex:form >
<apex:pageBlock title="Our Products">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!products}" var="pitem">
<apex:column headerValue="Product">
<apex:inputcheckbox value="{!mycheckval}" />
<apex:outputText value="{!pitem.Name}"/>
</apex:column>
<apex:column headerValue="Condition">
<apex:outputText value="{!pitem.Condition}"/>
</apex:column>
<apex:column headerValue="Price" style="text-align: right;">
<apex:outputText value="{0,number,currency}">
<apex:param value="{!pitem.Price}"/>
</apex:outputText>
</apex:column>
<apex:column headerValue="Qty to Buy">
<apex:inputText value="{!pitem.qtyToBuy}" rendered=" {!pitem.inStock}"/>
<apex:outputText value="Out of Stock" rendered="{! NOT(pitem.inStock)}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:commandButton action="{!addToCart}" value="Add to Cart" reRender="shopping_cart"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Your Cart" id="shopping_cart">
<apex:outputText value="{!cartContents}" escape="false"/>
</apex:pageBlock>
</apex:form>
</apex:page>
My Controller.
public class StoreFrontController
{
List<DisplayMerchandise> products;
List<DisplayMerchandise> shoppingCart = new List<DisplayMerchandise>();
// Action method to handle purchasing process
public PageReference addToCart()
{
for(DisplayMerchandise p : products)
{
if(0 < p.qtyToBuy)
{
shoppingCart.add(p);
}
}
return null; // stay on the same page
}
public String getCartContents()
{
if(0 == shoppingCart.size())
{
return '(empty)';
}
String msg = '<ul>\n';
for(DisplayMerchandise p : shoppingCart)
{
msg += '<li>';
msg += p.name + ' (' + p.qtyToBuy + ')';
msg += '</li>\n';
}
msg += '</ul>';
return msg;
}
public List<DisplayMerchandise> getProducts()
{
if(products == null)
{
products = new List<DisplayMerchandise>();
for(Merchandise__c item : [SELECT Id, Name, Description__c, Price__c, Total_Inventory__c
FROM Merchandise__c])
{
products.add(new DisplayMerchandise(item));
}
}
return products;
}
// Inner class to hold online store details for item
public class DisplayMerchandise
{
private Merchandise__c merchandise;
public DisplayMerchandise(Merchandise__c item)
{
this.merchandise = item;
}
// Properties for use in the Visualforce view
public String name
{
get { return merchandise.Name; }
}
public String condition
{
get { return merchandise.Description__c; }
}
public Decimal price
{
get { return merchandise.Price__c; }
}
public Boolean inStock
{
get { return (0 < merchandise.Total_Inventory__c); }
}
public Integer qtyToBuy { get; set; }
}
}
Aucun commentaire:
Enregistrer un commentaire