多个冰:SelectOneMenu选择

时间:2011-12-29 13:13:40

标签: java jsf datatable selectonemenu

我想知道如何在ice中实现多个选择:selectOneMenu。我有一个玩具清单,它是在启动背面豆时加载的,在UI中我有一个玩具桌,每排玩具都有一列selectOneMenu即玩具功能。我的问题是在每排玩具中展示选定的玩具功能。到目前为止,我能够使用以下代码在selectOneMenu中显示所选功能,但我不知道如何在不同的行上实现不同的选择我的意思是我只有一个属性“selectedToyFunction”映射到每一行。我需要实现java.util.Map<ToyId,ToyFunction>之类的东西。但我不知道如何以及在何处处理此类实施。

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

1 个答案:

答案 0 :(得分:0)

您有两个选择:

  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对象复制/移动所有更改。