了解SelectItemGroup [英] Understanding SelectItemGroup
问题描述
挖掘RadioRenderer
源代码,我注意到以下内容:
Digging into the RadioRenderer
source code I've noticed the following thing:
方法
@Override
protected void renderOption(FacesContext context,
UIComponent component,
Converter converter,
SelectItem curItem,
Object currentSelections,
Object[] submittedValues,
boolean alignVertical,
int itemNumber,
OptionComponentInfo optionInfo) throws IOException
从标准public void encodeEnd(FacesContext context, UIComponent component)
渲染器方法调用RadioRenderer
类中的
重写.但是有以下代码:
overriden in the RadioRenderer
class is being called from the standard public void encodeEnd(FacesContext context, UIComponent component)
Renderer method. But there was the following piece of code:
Iterator<SelectItem> items =
RenderKitUtils.getSelectItems(context, component);
//some code
while (items.hasNext()) {
SelectItem curItem = items.next();
idx++;
// If we come across a group of options, render them as a nested
// table.
if (curItem instanceof SelectItemGroup) {
// do some
else {
// do another
}
}
因此,我通过示例进行了尝试:
So, I tried it by the example:
<h:selectOneRadio>
<f:selectItem />
<f:selectItems value="#{myBean.vals}" />
<f:selectItems value="#{myBean.valss}" />
</h:selectOneRadio>
和selectItem
和selectItems
被视为不是SelectItemGroup
的实例.对于selectItem
来说,这很清楚,但是我希望selectItems
会映射到SelectItemGroup
实例.
and the selectItem
and the selectItems
es were treated as not the instances of the SelectItemGroup
. For selectItem
that's perfectly clear, but I expected that selectItems
would be mapped to the SelectItemGroup
instance.
你不能澄清一下吗?
推荐答案
不能以声明方式创建.它只能以编程方式创建. <f:selectItems>
还通过 javax.faces.model.SelectItem
支持List<SelectItem>
. > 实例,其中 SelectItemGroup
是一个子类.以下是其 javadoc :
It can not declaratively be created. It can only programmatically be created. The <f:selectItems>
also supports List<SelectItem>
with javax.faces.model.SelectItem
instances of which the SelectItemGroup
is a subclass. Below is an extract of relevance of its javadoc:
SelectItemGroup 是
UISelectMany
或
SelectItemGroup is a subclass of
SelectItem
that identifies a set of options that will be made available as a subordinate "submenu" or "options list", depending upon the requirements of theUISelectMany
orUISelectOne
renderer that is actually used. In general, the value property of this instance will be ignored, and the label property of this instance will be used to label the submenu.
这是如何创建它们的基本示例:
Here's a basic example of how you can create them:
private List<SelectItem> availableItems; // +getter
@PostConstruct
public void init() {
availableItems = new ArrayList<>();
SelectItemGroup group1 = new SelectItemGroup("Group 1");
group1.setSelectItems(new SelectItem[] {
new SelectItem("Group 1 Value 1", "Group 1 Label 1"),
new SelectItem("Group 1 Value 2", "Group 1 Label 2"),
new SelectItem("Group 1 Value 3", "Group 1 Label 3")
});
availableItems.add(group1);
SelectItemGroup group2 = new SelectItemGroup("Group 2");
group2.setSelectItems(new SelectItem[] {
new SelectItem("Group 2 Value 1", "Group 2 Label 1"),
new SelectItem("Group 2 Value 2", "Group 2 Label 2"),
new SelectItem("Group 2 Value 3", "Group 2 Label 3")
});
availableItems.add(group2);
}
<f:selectItems value="#{bean.availableItems}" />
内部 <thead>
,但这除外).最好将layout
设置为pageDirection
,否则所有内容都将显示在一行(lineDirection
)中.
Inside <h:selectOneRadio>
it will render as a nested table (which is IMHO a poor rendering, they'd better have rendered the group label as a <thead>
, but that aside). It's best visible when you set layout
to pageDirection
, otherwise everything will appear in a single line (lineDirection
).
<h:selectOneRadio layout="pageDirection">
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneRadio>
在 <h:selectOneMenu>
它将呈现为带有嵌套在<optgroup>
(这实际上是SelectItemGroup
的主要用例)中的选项值的树:
And inside the <h:selectOneMenu>
it will render as a tree with option values nested in <optgroup>
(which is actually the primary use case for SelectItemGroup
):
<h:selectOneMenu>
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>
The <h:selectManyListbox>
also supports it (the <h:selectOneListbox>
has exactly the same rendering, but supports only a single selection):
<h:selectManyListbox>
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>
<h:selectOneRadio>
相同的嵌套表渲染(看上去很尴尬):
The <h:selectManyCheckbox>
has the same (semantically awkward) nested table rendering as <h:selectOneRadio>
:
<h:selectManyCheckbox layout="pageDirection">
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyCheckbox>
The <h:selectManyMenu>
isn't useful at all, so I'll skip it.
这篇关于了解SelectItemGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!