2012-07-05 95 views
0

我不確定這是否可能,但我試圖獲取VisualForce Apex控制器類中動態創建的選擇列表的值。如何獲取動態創建的選擇列表組件的選定值

我使用下面的代碼爲特定對象(例如聯繫人)中的每個字段動態創建一個選擇列表,但現在我不知道如何獲取選定的值。我曾嘗試在構造函數中和單獨的行(不在下面的代碼示例中)中設置每個值,但這似乎不起作用。

VisualForce頁:

<apex:dynamicComponent componentValue="{!contactPageBlockSection}" /> 

的Apex控制器:

public Component.Apex.PageBlockSection GetContactPageBlockSection(string objectName) 
{ 
    Map<string, Schema.SObjectField> FieldMap; 
    FieldMap = Schema.SObjectType.Contact.fields.getMap(); 
    Set<string> FieldSet = FieldMap.keySet(); 
    List<string> FieldList = new List<string>(); 
    FieldList.addAll(FieldSet); 
    FieldList.sort(); 
    Component.Apex.PageBlockSection pbs = new Component.Apex.PageBlockSection(columns = 2); 

    for (string fieldName : FieldList) 
    { 
     Component.Apex.PageBlockSectionItem pbsi = new Component.Apex.PageBlockSectionItem(); 
     Schema.DescribeFieldResult field = (FieldMap.get(fieldName)).getDescribe(); 

     if (field.isUpdateable() && field.IsAccessible()) 
     { 
      Schema.DisplayType dt = field.getType(); 
      Component.Apex.OutputLabel lblText = new Component.Apex.OutputLabel(escape = false); 
      lblText.value = field.getLabel(); 
      pbsi.childComponents.add(lblText); 

      Component.Apex.SelectList selList = new Component.Apex.SelectList(id = field.getName(), multiselect = false, size = 1, style = 'width:200px;'); 

      if (dt == Schema.DisplayType.Integer || dt == Schema.DisplayType.Double || dt == Schema.DisplayType.Currency || dt == Schema.DisplayType.Percent) 
      { 
       AddSelectOption(selList, 'Keep highest value'); 
       AddSelectOption(selList, 'Keep lowest value'); 
       AddSelectOption(selList, 'Keep master value'); 
       pbsi.childComponents.add(selList); 
       pbs.childComponents.add(pbsi); 
      } 
     } 
    } 

    return pbs; 
} 

private void AddSelectOption(Component.Apex.SelectList selList, string option) 
{ 
    Component.Apex.SelectOption selOption = new Component.Apex.SelectOption(); 
    selOption.itemLabel = option; 
    selOption.itemValue = option; 
    selList.childComponents.add(selOption); 
} 

提前感謝

回答

0

只是用表達式語法動態創建的選擇列表值屬性綁定到一個字符串屬性如:

String selectedValue {get; set;} 
... 
setList.expressions.value = '{!selectedValue}'; 

該屬性應該存儲所選值,就像它在選擇列表的靜態聲明性定義中一樣。

+0

感謝您的回覆,但是當動態創建選擇列表時,我不知道該對象中有多少個字段,因此無法創建正確數量的變量來保存每個字段的值。我想知道是否有辦法直接引用頁面上的值或什麼? – 2012-07-06 08:42:56

0

您可以嘗試使用動態visualforce綁定。 I.E.

map<String,String> FieldToString {get; set;} 
... 
selList.expressions.value = '{!FieldToString[\'' + fieldname + '\']}'; 

但是,我從來沒有在動態visualforce中使用動態visualforce綁定,所以警告emptor。

相關問題