我有一个带有 <h:dataTable>
的 Facelets 页面.在每一行中都有一个 <h:selectBooleanCheckbox>
.如果选中复选框,则应在 bean 中设置相应行后面的对象。
<h:selectManyCheckbox>
做会更好吗? ? 最佳答案
最好的办法是绑定(bind) h:selectBooleanCheckbox
带有 Map<RowId, Boolean>
的值属性 RowId
表示行标识符的类型。举个例子,你有一个 Item
其标识符属性 id
的对象是 Long
:
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:selectBooleanCheckbox value="#{bean.checked[item.id]}" />
</h:column>
...
</h:dataTable>
<h:commandButton value="submit" action="#{bean.submit}" />
这将与以下内容结合使用:
public class Item {
private Long id;
// ...
}
和
public class Bean {
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
private List<Item> items;
public void submit() {
List<Item> checkedItems = checked.entrySet().stream()
.filter(Entry::getKey)
.map(Entry::getValue)
.collect(Collectors.toList());
checked.clear(); // If necessary.
// Now do your thing with checkedItems.
}
// ...
}
你看, map 自动填充
id
的所有表项作为键,复选框值自动设置为与项相关的映射值 id
作为关键。
关于jsf - 如何使用 <h :selectBooleanCheckbox> in <h:dataTable> or <ui:repeat> to select multiple items?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2524514/