You need to sign in to do that
Don't have an account?
Using commandlink to read a value from inputtext within a repeat
Have seen similar posts and tried to follow the solution - but clearly I'm still missing something. Hoping one of you lovely people can help me out.
I am using an apex repeat to display a list of product details (using a custom class, not Product2). I want to be able to enter a value for Quantity and click on a commandlink to add a product from the list to a 'shopping cart'. I have no problem getting the outputtext data across, but cannot get the value I entered in the inputtext Quantity field.
A posted solution was to use custom getters/setter methods; I tried this and can indeed see my entered value in the debug log. So far so good, however I cannot figure out how to access this in my code!
Enough wittering, here's the code:
<apex:repeat value="{!mp_liAvailableProducts}" var="AvailableProd">
<apex:commandLink action="{!actionAddProduct}" value="Add" id="theAddProductLink" >
<apex:param name="AddProdId" value="{!AvailableProd.strProdId}" />
<apex:param name="AddProdQuantity" value="{!AvailableProd.iProdQuantity}" />
</apex:commandLink>
<apex:inputText value="{!AvailableProd.iProdQuantity}" size="2" maxlength="2" />
<a href="{!URLFOR($Action.Product2.View, AvailableProd.strProdId)}" target="_blank"> {!AvailableProd.strProdName} </a>
<apex:outputText value="{!AvailableProd.strProdCode}" />
<apex:outputText value="{!AvailableProd.strProdDesc}"/>
</apex:repeat>
public class SPSelectProducts
{
public String strProdId {get; set;}
public String strProdName {get; set;}
public String strProdCode {get; set;}
public integer iProdQuantity;
public string strProdDesc {get; set;}
public SPSelectProducts(string ProductId, String ProductName, String ProductCode, integer ProductQuantity, string ProductDesc)
{
strProdId = ProductId;
strProdName = ProductName;
strProdCode = ProductCode;
iProdQuantity = ProductQuantity;
strProdDesc = ProductDesc;
}
// Custom setters for Quantity
public integer getiProdQuantity()
{
return this.iProdQuantity;
}
public void setiProdQuantity(Integer value)
{
iProdQuantity = value;
System.debug('****** Inside setter *****' + value);
}
}
public PageReference actionAddProduct()
{
SPSelectProducts sSPSP = m_mapAvailProds.get(ApexPages.currentPage().getParameters().get('AddProdId'));
sSPSP.iProdQuantity = integer.valueOf(ApexPages.currentPage().getParameters().get('AddProdQuantity'));
At this point the 'AddProdQuantity' parameter is 0, which is why I went down the getter/setter method route.
The list mp_liAvailableProducts has all the product details with Quantity = 0.
Yet the ****** Inside setter ***** debug statement shows the value I entered in the list. This method is called once for each entry in the list, my entered value appears in the correct iteration.
Thanks in advance for your help,
Cheers
Mike
The issue here is that the commandLink is generated before the user enter any value into the inputText.
One way to get the behavior you wanted is to use <apex:actionFunction> to refresh the commandLink after the value is changed.
Example:
I had the same issue before and tried in the following way and it worked for me
<apex:repeat value="{!mp_liAvailableProducts}" var="AvailableProd">
<apex:commandLink action="{!actionAddProduct}" value="Add" id="theAddProductLink" >
<apex:param name="AddProdId" value="{!AvailableProd.strProdId}" assignTo="{!ctrlProID}" />
<apex:param name="AddProdQuantity" value="{!AvailableProd.iProdQuantity}" assignTo="{!ctrliPorQuantity}" />
</apex:commandLink>
<apex:inputText value="{!AvailableProd.iProdQuantity}" size="2" maxlength="2" />
<a href="{!URLFOR($Action.Product2.View, AvailableProd.strProdId)}" target="_blank"> {!AvailableProd.strProdName} </a>
<apex:outputText value="{!AvailableProd.strProdCode}" />
<apex:outputText value="{!AvailableProd.strProdDesc}"/>
</apex:repeat>
Controller:
public String ctrlProID {get;set;}
public Integer ctrliProQuantity {get;set;}
Thanks for the help guys! Sadly I ran out of time, so I used a list with a checkbox instead. Will try your suggestions next time...