function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SGrabo_NutriciaSGrabo_Nutricia 

Binding SelectList control to custom object

I'm a newbie to VF, so please be gentle...

 

I'm attempting to have a dropdown SelectList presented in a PageBlockTable so that adding new mass-insert records can be typified by a value drawn from another table. 

 

I have a simple custom object (Details) with only two editable fields: quantity and product ID. The product ID is to be drawn from a dropdown built from the Product2 table. I'd like to have the ID value of the product bound to the Details.ProductID__c value. This should be done in-line with the entry of the quantity, but I can't get the product SelectList to appear within the PageBlockTable.

 

My Apex:

 

<apex:page controller="DetailTesting"> <apex:form > <apex:PageBlock title="Products with a product type of 'Product'" tabstyle="Product2"> <apex:pageBlockButtons location="top"> <apex:commandButton value="Save" action="{!save}" rerender="error"/> </apex:pageBlockButtons> <b>Products</b><apex:selectList value="{!productList}" size="1"> <apex:selectOptions value="{!lowProteinProducts}"/> </apex:selectList> <apex:pageBlockTable value="{!sampleLowProtein}" var="a" id="table"> <apex:facet name="footer"> <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error"/> </apex:facet> <apex:column headerValue="Quantity"> <apex:inputField value="{!a.Quantity__c}"/> </apex:column> <apex:column headerValue="Name"> <apex:inputField value="{!a.Name}"/> </apex:column> </apex:pageBlockTable> </apex:PageBlock> </apex:form> </apex:page>

And my controller:

 

public class DetailTesting{ public String family; public List<Product2> productList = [select id, name, family from product2 order by name]; public List<Sample_Detail__c> sampleLowProtein{get; set;} public DetailTesting(){ sampleLowProtein = new List<Sample_Detail__c>(); sampleLowProtein.add(new Sample_Detail__c()); } public List<SelectOption> getLowProteinProducts(){ List<SelectOption> lowProteinProducts = new List<SelectOption>(); for (Integer i=0; i < productList.size() ; i++){ if (productList.get(i).family == 'Low Protein') lowProteinProducts.add(new SelectOption(productList.get(i).Id, productList.get(i).Name)); } return lowProteinProducts; } public void addRow(){ sampleLowProtein.add(new Sample_Detail__c()); } public PageReference save(){ insert sampleLowProtein; PageReference home = new PageReference('/home/home.jsp'); home.setRedirect(true); return home; } public List<Product2> getProductList() {return productList;} public void setProductList(List<Product2> productList) {this.productList = productList;} }

When I attempt to put LowProteinProducts inside the PageBlockTable, it does not appear. I assume this is because the PageBlockTable value is "{!sampleLowProtein}" while the SelectList value is "{!productList}".

 

I'm sure there's something obvious I'm missing, but help is clearly needed. How can I work around this?

 

 

 

 

jwetzlerjwetzler

Did you put it inside of a column component when you have it inside the pageBlockTable?

 

Also two things:

1) If you're going to put the selectList inside of an iterating component, it could get pretty expensive to keep building that list every time.  Since the products in your list aren't going to change I would suggest lazy loading it.

Something like this would work:

 

List<SelectOption> lowProteinProducts = null;

 

public List<SelectOption> getLowProteinProducts(){

if (lowProteinProducts = null) {

lowProteinProducts = new List<SelectOption(); for (Integer i=0; i < productList.size() ; i++){ if (productList.get(i).family == 'Low Protein') lowProteinProducts.add(new SelectOption(productList.get(i).Id, productList.get(i).Name)); } }

return lowProteinProducts; 

 

2) You need to bind your selectList's value attribute to something correct.  In the example you posted, you should be binding to a String property, not a list.  If you're going to do this in an iteration though, you'll probably want to bind to a field on your object, like {!a.productid__c} or something similar.

 

SGrabo_NutriciaSGrabo_Nutricia

Terrific! That's gotten me several steps closer. I'm now able to save the record, and it's correctly assigning the ID value from the droplist to the ProductID__c column of my object. This, then, raises the following questions:

 

 

I'd like to use the name (displayed) column of the droplist as the value for the Name column in the object upon save. Further, I need to know how to identify each instance of the object with the appropriate row among the several the user may create.

 

I think I understand how to do the latter (sampleLowProtein.get(i).Name within a do loop). What I'm not sure of is twofold:

 

1. how to reference the displayed column of a given SelectList, and

2. how to reference the instance of the SelectList (to corresond to get(i) of the data object).

 

Thoughts?