2011-12-29 77 views
0

我想知道如何在冰中實現多重選擇:selectOneMenu。我列出了在輔助豆的啓動時加載的玩具列表,並且在UI中,我有一張玩具表,其中每行玩具有一列selectOneMenu,即玩具功能。我的問題是在每排玩具中顯示選定的玩具功能。到目前爲止,我可以在selectOneMenu中用下面的代碼顯示選定的函數,但我不知道如何在不同的行上實現不同的選擇,我的意思是我只有一個屬性「selectedToyFunction」,它映射到每一行。我需要實現類似java.util.Map<ToyId,ToyFunction>。但我不知道如何以及在哪裏處理這種實現。多重冰:SelectOneMenu選擇

JSPX

<ice:dataTable border="0" value="#{myBean.toys}" var="toy" scrollable="false" resizable="false"> 
    <ice:column id="toyDetailRedirect" styleClass="smallColumn"> 
     <ice:selectOneMenu styleClass="inputCombo" partialSubmit="true" valueChangeListener="#{myBean.redirectToToyFunctionDetail}" value="#{myBean.selectedToyFunction}"> 
      <f:attribute name="toy" value="#{toy.id}" /> 
      <f:selectItems value="#{myBean.toyFunctions}" /> 
     </ice:selectOneMenu> 
     <f:facet name="header"> 
      <ice:outputText value="Details" /> 
     </f:facet> 
    </ice:column> 
<ice:dataTable> 

BackingBean

public class MyBean 
{  
    //--- Services --- 
    private ToyService toyService;  

    //---- UI ----- 
    private String selectedToyFunction; 
    private List<SelectItem> toyFunctions = new ArrayList<SelectItem>(); 

    //--- properties 
    private List<Toy> toys = new ArrayList<Toy>(); 
    private static final String DEFAULT_SELECTION ="--- Select ---"; 
    private static final String FUNCTION_A ="A"; 
    private static final String FUNCTION_B ="B"; 
    private static final String FUNCTION_C ="C"; 

    // ---- Logging ---- 
    private final Logger logger = Logger.getLogger(MyBean.class); 


    public MyBean() 
    { 
     super(); 
    } 

    @PostConstruct 
    public void loadToysAndPopulateToysFunctions() 
    {  
     // loadToys 
     try 
     { 
      this.toys = this.toysService.findAllToys(); 
     } 
     catch (Exception e) 
     { 
      this.logger.warn(e.getMessage()); 
      this.logger.debug(e); 
     } 

     if ((this.toys == null) || this.toys.isEmpty()) 
     { 
      /* Out if not toy has been found */ 
      logger.debug("No Toy has been found !"); 
      return; 
     } 
     // Populate default toy functions 
     this.populateToyFunctions(); 
    }  

    private void populateToyFunctions() 
    {  
     if ((this.toyFunctions == null) || this.toyFunctions.isEmpty()) 
     { 
      this.toyFunctions = new ArrayList<SelectItem>(); 
      this.toyFunctions.add(new SelectItem(DEFAULT_SELECTION)); 
      this.toyFunctions.add(new SelectItem(FUNCTION_A)); 
      this.toyFunctions.add(new SelectItem(FUNCTION_B)); 
      this.toyFunctions.add(new SelectItem(FUNCTION_C));    
     } 
     //Default selection 
     this.selectedToyFunction = DEFAULT_SELECTION;  
    } 

    public void redirectToToyFunctionDetail(ValueChangeEvent e) 
    { 
     FacesContext.getCurrentInstance() 
          .getExternalContext() 
          .redirect("/Store/Details.jspx?id=" +e.getNewValue().toString());  
    } 

    // ---- Getters & Setters ----- 

    public List<Toy> getToys() 
    { 
     return this.toys; 
    } 

    public void setToys(List<Toy> toys) 
    { 
     this.toys = toys; 
    }  

    public List<SelectItem> getToyFunctions() 
    { 

     return this.toyFunctions; 
    } 

    public void setToyFunctions(List<SelectItem> toyFunctions) 
    { 
     this.toyFunctions = toyFunctions; 
    } 

    public void setSelectedToyFunction(String selectedToyFunction) 
    { 
     this.selectedToyFunction = selectedToyFunction; 
    } 

    public String getSelectedToyFunction() 
    { 
     return this.selectedToyFunction; 
    }  
} 

回答

0

你有2種選擇:

  1. 就值綁定到當前迭代Toy對象:

    <ice:selectOneMenu value="#{toy.function}"> 
    

    ,並給予Toy類相應的屬性,如果尚未:

    private String function; 
    

    這是自然的方法。

  2. 綁定值來共同Map bean屬性如你所說自己:

    <ice:selectOneMenu value="#{myBean.selectedToyFunctions[toy.id]}"> 
    

    確保你已經準備HashMap事先:

    private Map<Long, String> selectedToyFunctions = new HashMap<Long, String>(); 
    

    這僅僅是醜陋的做法。在準備/保存之前,您可能需要手動將所有更改從/移到真實的Toy對象中。

+0

謝謝,第二個選項是我尋找的線索。 – Rehman 2011-12-29 14:32:28