2012-03-06 24 views
3

こんにちは私は、Visualforceのリピータコントロールと頭痛を取得しています:私が何をしようとしています何Visualforceのリピータ

<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> 

は、商品詳細リスト内の各レコードに対して、テキストボックスを生成しています。このテキストボックスは、別のフィールド(theList.quantity)にバインドされています。しかし、最後のテキストボックスの値を変更すると、すべてのテキストボックスに量が設定されることがわかりました(明らかに、同じフィールドにバインドされているため)。

私の質問は、リピータで生成された各テキストボックスに独自のパラメータ値を持たせる最も良い方法は何ですか?

間違った方法でリピータを使用していますか?

EDIT(明確化):

各製品の詳細記録のために(私はを反復処理しています何)私は、ユーザーが製品の数量を入力することができますテキストボックスを持っていると思います。このテキストボックスの値は、製品詳細レコードとは無関係です。

私の質問は、どのようにテキストボックスの反復ごとに一意のパラメータを生成できますか?うまくいけばそれは理にかなっている。あなたはproductDetailsリストの各要素に対して<apex:repeat>varを使用する必要が

乾杯

答えて

2

これを行う最も簡単な方法は、繰り返すオブジェクトにカスタムフィールドを追加することです。しかし、それは望ましくない場合があることを理解しています。

おそらくカスタムのApexオブジェクト(カスタムSalesforceオブジェクトではない)を使用することができます。コントローラまたはエクステンション内にネストされたクラスを追加することで、これを行うことができます。例えば

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;} 
} 

あなたは(SOQLから返された)あなたの製品の詳細レコードのすべてをループする必要がある、とMyProductDetailsリストに追加します。しかし、それを持っていれば、Visualforce Repeaterを使用してそれぞれを表示し、各レコードの入力量データを保存することができます。

<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> 

希望します。

+0

こんにちは、あなたのコードに従いましたが、不明なプロパティ 'ImageController.Product.name'を取得しています – Jim

+1

サブクラスのプロパティはおそらく{get; set;}の後に必要です。すみません、私はそれらを忘れました。 –

+0

固定してありがとうトン! – Jim

3

。例えば:各productDetailquantityプロパティを設定します

<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> 

この例ではproductDetailの親を実際に反復処理している場合は、コントローラを変更してそれぞれの親を作成し、それを反復処理する必要があります。私は、潜在的な注文のリストを繰り返しているかのようにサンプルコードを書きます。

コントローラでは、各製品の注文を作成する必要があります。親がSObjectクラスかカスタムクラスかはわかりませんが、カスタムクラスのように記述します。

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; 
} 

今、あなたのVisualforceページであなたはOrderリストを反復処理し、ユーザーが数量を設定することができます。

<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> 

戻るあなたのコントローラでは、唯一のゼロ以上(または他のどんな基準あなたは心を持っている)のquantityを持ってordersを保存します。

+0

お返事ありがとうございます - productDetailはquantityというフィールドを持っていません、私はproductDetailレコードごとに新しいテキストボックスを生成したかっただけです。 – Jim

+0

そのテキストボックスに何が欲しいのですか? – barelyknown

+0

任意のパラメータです。したがって、ユーザーがコードビハインドで入力した値にアクセスできます。 – Jim