简体   繁体   English

如何选择多个行 <h:dataTable> 同 <h:selectBooleanCheckbox>

[英]How to select multiple rows of <h:dataTable> with <h:selectBooleanCheckbox>

I use <h:dataTable> to list data from database. 我使用<h:dataTable>列出数据库中的数据。 We have many records in page, now I would like to select multiple records with a checkbox in each row. 页面中有很多记录,现在我想选择每行带有一个复选框的多个记录。 How can I achieve this? 我该如何实现?

I assume that your entity is that well-designed that it has an unique technical identifier, for example the auto increment sequence from the DB. 我假设您的实体经过精心设计,具有唯一的技术标识符,例如数据库中的自动递增序列。

public class Entity {

    private Long id;
    // ...
}

If not, you'll need to add it. 如果没有,则需要添加它。

Then, add a Map<Long, Boolean> property to the bean which is tied to the table. 然后,将Map<Long, Boolean>属性添加到与表绑定的bean。

private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

(preinitialization can also happen in (post)constructor, take your pick, at least JSF won't do it for you; oh, give it a getter as well, a setter is not necessary) (预初始化也可以在(后)构造函数中发生,请选择,至少JSF不会为您完成;哦,也给它做一个吸气剂,则不需要一个setter)

Then, add a column with a checkbox which maps to the boolean value by entity ID as key. 然后,添加带有复选框的列,该复选框通过实体ID作为键映射到布尔值。

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        <h:selectBooleanCheckbox value="#{bean.checked[entity.id]}" />
    </h:column>
    ...
</h:dataTable>
<h:commandButton value="Delete" action="#{bean.delete}" />

Now, in the action method associated with the delete button, you can collect and delete the checked items as follows: 现在,在与“删除”按钮关联的操作方法中,您可以按以下方式收集和删除选中的项目:

public void delete() {
    List<Entity> entitiesToDelete = new ArrayList<Entity>();

    for (Entity entity : entities) {
        if (checked.get(entity.getId())) {
            entitiesToDelete.add(entity);
        }
    }

    entityService.delete(entitiesToDelete);
    checked.clear();
    loadEntities();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用<h:selectBooleanCheckbox>在<h:dataTable>或者<ui:repeat>选择多个项目? - How to use <h:selectBooleanCheckbox> in <h:dataTable> or <ui:repeat> to select multiple items? 使用h:selectBooleanCheckbox删除JSF数据表中的多行 - Using h:selectBooleanCheckbox to delete multiple rows in JSF datatable 如何在ah:dataTable中的ah:dataTable中映射ah:selectBooleanCheckbox的值? - How to map the value of a h:selectBooleanCheckbox in a h:dataTable within a h:dataTable? h:selectBooleanCheckBox对选择的操作 - h:selectBooleanCheckBox action on select 如何设置ah:selectBooleanCheckbox的边框样式? - How to style the border of a h:selectBooleanCheckbox? 如何使用位于数据表ah:列中的h:selectbooleancheckbox onclick方法来填充InputText? - How to use h:selectbooleancheckbox onclick method located in a h:column of a dataTable in order to fill an InputText? 如何使用JSF的h:selectBooleanCheckbox和h:dataTable来获取选中的复选框而不提交页面内容? - How to use JSF's h:selectBooleanCheckbox with h:dataTable to get selected checkboxes without submitting the page content? JSF:如何绑定许多h:selectBooleanCheckbox? - JSF: How to bind many of h:selectBooleanCheckbox? 运用 <h:selectBooleanCheckbox> 在分页中使用地图 <p:dataTable> 抛出NullPointerException - Using <h:selectBooleanCheckbox> with Map in a paginated <p:dataTable> throws NullPointerException 使用h:selectBooleanCheckbox和c:if来获取a的行值 <rich:datatable> -JSF - Using h:selectBooleanCheckbox with c:if to obtain row value of a <rich:datatable> - JSF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM