简体   繁体   English

如何将PrimeFaces SelectOneMenu ItemLabel和ItemValue与列表一起使用?

[英]How to use PrimeFaces SelectOneMenu ItemLabel & ItemValue with Lists?

As far as im aware the correct way to display information in a SelectOneMenu is by having a list of objects and using it's properties like so: 我知道在SelectOneMenu中显示信息的正确方法是通过获取对象列表并使用它的属性,如下所示:

<p:selectOneMenu id="player" value="">  
    <f:selectItem itemLabel="Select" itemValue="" />  
    <f:selectItems value="#{players}"
       var="player"
       itemLabel="#{player.name}"
       itemValue="#{player.id}"/> 
</p:selectOneMenu> 

but what if I dont have a list of players, what if I have something like this? 但是,如果我没有一个球员名单,如果我有这样的东西怎么办? I'd like to get it to work like this: 我想让它像这样工作:

//PlayerManager
public List<String> getPlayerNames() {
    String[] names = new String[] {"player1", "player2"};
    return Arrays.asList(names);
}

public List<String> getPlayerIds() {
   String[] ids = new String[] {"1", "2"};
   return Arrays.asList(ids);
}

<p:selectOneMenu id="player" value="">  
    <f:selectItem itemLabel="Select" itemValue="" />  
    <f:selectItems value="#{playerManager.playerNames}"
       var="player"
       itemLabel="#{playerManager.playerNames}"
       itemValue="#{playerManager.playerIds}"/> 
</p:selectOneMenu> 

Use <c:forEach> to generate <f:selectItem> components. 使用<c:forEach>生成<f:selectItem>组件。 You can use its varStatus attribute to get the current iteration index, so that you can get an item of the other list by index. 您可以使用其varStatus属性来获取当前迭代索引,以便您可以通过索引获取另一个列表的项目。

<c:forEach items="#{playerManager.playerIds}" var="playerId" varStatus="loop">
    <f:selectItem itemValue="#{playerId}" itemLabel="#{playerManager.playerNames[loop.index]}" />
</c:forEach>

Note that when the #{playerManager} is view scoped, this construct would then recreate the bean on every postback. 请注意,当#{playerManager}是视图范围时,此构造将在每次回发时重新创建bean。 See also JSTL in JSF2 Facelets... makes sense? 另请参阅JSF2 Facelets中的JSTL ......有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM