java 如何在列表中使用 PrimeFaces SelectOneMenu ItemLabel & ItemValue?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15815517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PrimeFaces SelectOneMenu ItemLabel & ItemValue with Lists?
提问by Jonathan
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>
回答by BalusC
Use <c:forEach>
to generate <f:selectItem>
components. 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.
使用<c:forEach>
产生<f:selectItem>
的部件。可以使用它的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. See also JSTL in JSF2 Facelets... makes sense?
请注意,当#{playerManager}
is 视图范围时,此构造将在每次回发时重新创建 bean。另请参阅JSF2 Facelets 中的 JSTL... 有意义吗?