2013-02-21 57 views
0

我有一個ice:selectOneMenu組件並需要獲得從頁面中選擇的ID和值:獲取標籤的ID和值在冰

<ice:selectOneMenu partialSubmit="true" 
value="#{bean.selectedType}" valueChangeListener="#{bean.listenerSelectedType}"> 
<f:selectItems value="#{bean.typeValues}"/> 
<ice:selectOneMenu/> 


public List<?> getTypeValues(){ 
List<SelectItem> returnList = new ArrayList<SelectItem>(); 
... 
//SelectItem item = new SelectItem(id, label); 
SelectItem item = new SelectItem("A", "B"); 

returnList.add(item); 
} 

public void listenerSelectedType(ValueChangeEvent event) { 
    ... 
    //The event only has the id ("A") 
    //How can I get the label ("B") that is in the page? 
} 

回答

0

這是真實的,在提交表單只值<select> HTML元素將被髮送到服務器。

但是,只要您是使用值和標籤屬性填充了selectOneMenu,如果您遍歷所創建的集合以找到所需內容,也可以訪問此標籤。

簡而言之,請記住您在bean中創建的集合並遍歷它以獲取標籤。這是一個基本的例子:

@ManagedBean 
@ViewScoped 
public void MyBean implements Serializable { 

    private List<SelectItem> col; 

    public MyBean() { 
     //initialize your collection somehow 
     List<SelectItem> col = createCollection();//return your collection 
     this.col = col; 
    } 

    public void listenerSelectedType(ValueChangeEvent event) { 
     String value = (String)event.getNewValue(); 
     String label = null; 
     for(SelectItem si : col) { 
      if(((String)si.getValue()).equals(value)) { 
       label = si.getLabel(); 
      } 
     } 
    } 

} 

順便說一句,一定要初始化您的收藏在類的構造函數或在@PostConstrct方法和getter方法不這樣做(業務)的工作 - 這是a bad practice

同時,實現您selectOneMenu與背襯Map<String, String> options可能是一個更好的選擇,因爲標籤將通過一個簡單的調用是可訪問:String label = options.get(value),假設你的地圖包含<option value, option label>作爲地圖的<key, pair>

+0

感謝您的回覆。這是我實施的解決方案。我認爲還有另一種方法可以在不迭代的情況下獲得標籤值。 – user2095246 2013-02-22 11:27:36

+0

不客氣。您所說的解決方案基於使用「Map」實例來保存數據。 – skuntsel 2013-02-22 11:42:56

+0

此外,您可以選擇答案作爲接受,如果它幫助你解決你的問題。 – skuntsel 2013-02-22 11:43:40