Visualforce Repeaters

Hi I am getting a headache with the visualforce relay control:

 <apex:repeat value="{!productDetails}" var="o">
     <apex:pageBlockSectionItem >
         <apex:outputText style="label" value="Name:"></apex:outputText>
         <apex:outputLabel value="{!o.name}" /> 
     </apex:pageBlockSectionItem>

     <apex:pageBlockSectionItem >
         <apex:outputText value="{!o.ProductCode}"/>   
         <apex:outputText value="{!o.Family}" />  
     </apex:pageblockSection> 

     <apex:pageBlockSection> 
         <apex:outputText style="label" value="Quantity:"> </apex:outputText>
         <apex:inputText value="{!theList}"/>
     </apex:pageblockSection> 
 </apex:repeat>

      

What I am trying to do, for every entry in the product details list, generates a text box. This textbox is bound to another field (theList.quantity). But I find that when I change the value in the last textbox it sets the count in all the textboxes (obviously, since they are associated with the same field).

So my question is, what is the best way to have each textbox that is generated in the repeater has its own parameter value?

Or am I using the repeater incorrectly?

EDIT (clarification):

For each product detail record (which I repeat) I want to have a text box where the user can enter the number of products. The value of this text box is unrelated to product detail data.

My question is, how can I generate unique parameters for each iteration of the textbox? Hope this makes sense.

Greetings

+3


source to share


2 answers


The easiest way to do this is to add a custom field to the object you are repeating. However, I understand that there are cases where this is not desirable.

You can probably use a custom Apex object for this (not a custom Salesforce object). You can do this by adding a nested controller inside your controller or extension.

For example:

public class MyClass 
{
    public class MyProductDetail
    {
        public string Name {get;set;}
        public string Family {get;set;}
        public string ProductCode {get;set;}
        public integer Quantity {get;set;} // theList
    }

    public List<MyProductDetail> MyProductDetails {get;set;}
}

      



You will need to go through all of your product detail records (returned from SOQL) and add them to the MyProductDetails list. However, if you have this, you can use the Visualforce Repeater to display each one and save the entered count data for each entry.

<apex:repeat value="{!MyProductDetails}" var="o">
    <apex:pageBlockSectionItem >
        <apex:outputText style="label" value="Name:"></apex:outputText>
        <apex:outputLabel value="{!o.Name}" /> 
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
        <apex:outputText value="{!o.ProductCode}"/>   
        <apex:outputText value="{!o.Family}" />  
    </apex:pageblockSection> 

    <apex:pageBlockSection> 
        <apex:outputText style="label" value="Quantity:"> </apex:outputText>
        <apex:inputText value="{!o.Quantity}"/>
    </apex:pageblockSection> 
</apex:repeat>

      

Hope it helps!

+2


source


You need to use var

in <apex:repeat>

for every item in the list productDetails

. For example:

<apex:repeat value="{!productDetails}" var="productDetail">
    <apex:pageBlockSection> 
        <apex:outputText style="label" value="Quantity:"> </apex:outputText>
        <apex:inputText value="{!productDetail.quantity}"/>
    </apex:pageblockSection> 
</apex:repeat>

      

This will set the property quantity

for everyone productDetail

.

If you are indeed repeating the parent productDetail

in this example, you will need to modify your controller to create a parent for each and then iterate over it. I'll write the example code as if you were repeating a list of potential orders.

In your controller, you will need to create an order for each of the products. I'm not sure if the parent is SObject or a custom class, but I'll write an example as if it were a custom class.



public class Order {
    public Order(ProductDetail productDetail) {
        this.productDetail = productDetail;
    }

    public ProductDetail productDetail;
    public Integer quantity;
}

// I assume you've already implemented this getter or are setting it some other way.
public ProductDetail[] productDetails {
    get {
        if (productDetails == null) {
            productDetails = ...;
        }
        return productDetails;
    }
    set;
}

public Order[] orders {
    get {
        if (orders == null) {
            orders = new Order[]{};
            for (ProductDetail productDetail: productDetails) {
                orders.add(new Order(productDetail);
            }
        }
        return orders;
    }
    set;
}

      

Now, in your VisualForce page, you can iterate over the list Order

and set the amount by the user.

<apex:repeat value="{!orders}" var="order">
    ...
    <apex:outputtext style="label" value="Name:"></apex:outputText>
    <apex:outputLabel value="{!order.productDetail.name}" /> 
    ...
    <apex:inputText value="{!order.quantity}"/>
    ....
</apex:repeat>

      

Go back to your controller, save orders

which is quantity

greater than zero (or whatever criteria you mean).

+3


source







All Articles