gpt4 book ai didi

jsf - 了解 SelectItemGroup

转载 作者:行者123 更新时间:2023-12-01 22:27:22 25 4
gpt4 key购买 nike

深入研究 RadioRenderer 源代码,我注意到以下内容:

方法

@Override
protected void renderOption(FacesContext context,
UIComponent component,
Converter converter,
SelectItem curItem,
Object currentSelections,
Object[] submittedValues,
boolean alignVertical,
int itemNumber,
OptionComponentInfo optionInfo) throws IOException

RadioRenderer 类中的覆盖是从标准 public void encodeEnd(FacesContext context, UIComponent component) Renderer 方法调用的。但是有如下一段代码:

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

因此,我通过示例进行了尝试:

<h:selectOneRadio>
<f:selectItem />
<f:selectItems value="#{myBean.vals}" />
<f:selectItems value="#{myBean.valss}" />
</h:selectOneRadio>

并且 selectItemselectItemses 被视为不是 SelectItemGroup 的实例。对于 selectItem 这非常清楚,但我预计 selectItems 会映射到 SelectItemGroup 实例。

你不能澄清一下吗?

最佳答案

它不能以声明方式创建。它只能以编程方式创建。 <f:selectItems>还支持 List<SelectItem> javax.faces.model.SelectItem SelectItemGroup 的实例是一个子类。以下是其 javadoc 的相关摘录:

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 the UISelectMany or UISelectOne 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.

以下是如何创建它们的基本示例:

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}" />

内部 <h:selectOneRadio> 它将呈现为嵌套表(恕我直言,这是一个糟糕的呈现,他们最好将组标签呈现为 <thead> ,但除此之外)。当你设置 layout 时它最明显至 pageDirection , 否则所有内容都将显示在一行中 ( lineDirection )。

<h:selectOneRadio layout="pageDirection">
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneRadio>

<h:selectOneRadio> with SelectItemGroup


<h:selectOneMenu> 里面它将呈现为一棵树,选项值嵌套在 <optgroup> 中(这实际上是 SelectItemGroup 的主要用例):

<h:selectOneMenu>
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

<h:selectOneMenu> with SelectItemGroup


<h:selectManyListbox> 也支持它( <h:selectOneListbox> 具有完全相同的渲染,但仅支持单选):

<h:selectManyListbox>
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>

<h:selectManyListbox> with SelectItemGroup


<h:selectManyCheckbox> 具有与 <h:selectOneRadio> 相同的(语义上尴尬的)嵌套表格呈现:

<h:selectManyCheckbox layout="pageDirection">
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyCheckbox>

<h:selectManyCheckbox> with SelectItemGroup


<h:selectManyMenu> 根本没有用,所以我将跳过它。

另见:

关于jsf - 了解 SelectItemGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com