0

是否可以使用标签selectselected="selected"属性中呈现许多选项?f:selectItem/f:selectItems

我想生成用于jQuery UI MultiSelect Widget的选择,示例代码应如下所示:

<select name="example-presets" multiple="multiple" size="5">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3" selected="selected">Option 3</option>
    <option value="option4" selected="selected">Option 4</option>
    <option value="option5" disabled="disabled">Option 5</option>
    <option value="option6" disabled="disabled">Option 6</option>
    <option value="option7">Option 7</option>
    <option value="option8">Option 8</option>
    <option value="option9">Option 9</option>
</select>

我知道我可以使用ui:repeat或编写自定义组件来做到这一点......

4

1 回答 1

2

在 JSF 上下文中交谈时,您通常通过<h:selectManyListbox value>预先UIInput

使用此视图可以实现所需的 HTML 输出

<h:selectManyListbox id="example-presets" value="#{bean.selectedItems}" size="5">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>

这个模型在#{bean}

private List<String> selectedItems; // +getter+setter
private List<SelectItem> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    selectedItems = Arrays.asList("option1", "option3", "option4");
    availableItems = Arrays.asList(
        new SelectItem("option1", "Option 1", null, false),
        new SelectItem("option2", "Option 2", null, false),
        new SelectItem("option3", "Option 3", null, false),
        new SelectItem("option4", "Option 4", null, false),
        new SelectItem("option5", "Option 5", null, true),
        new SelectItem("option6", "Option 6", null, true),
        new SelectItem("option7", "Option 7", null, false),
        new SelectItem("option8", "Option 8", null, false),
        new SelectItem("option9", "Option 9", null, false)
    );
}

生成的 HTML 输出为:

<select id="form:example-presets" name="form:example-presets" multiple="multiple" size="5">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3" selected="selected">Option 3</option>
    <option value="option4" selected="selected">Option 4</option>
    <option value="option5" disabled="disabled">Option 5</option>
    <option value="option6" disabled="disabled">Option 6</option>
    <option value="option7">Option 7</option>
    <option value="option8">Option 8</option>
    <option value="option9">Option 9</option>
</select>

就像你的意图一样。顺便说一句,确保您的 jQuery 选择器是正确的。另请参阅如何使用 jQuery 选择 JSF 组件?

于 2013-07-24T12:12:44.350 回答