实现“选择所有复选框”在丰富:数据表标题

发布于 2025-01-08 08:07:10 字数 1649 浏览 0 评论 0原文

我正在尝试在 rich:dataTable 标头中实现“选择所有复选框”复选框,而不使用 JavaScript。

XHTML:

<rich:column styleClass="center-aligned-text">
    <f:facet name="header">
        <h:selectBooleanCheckbox id="selectAll" title="selectAll" valueChangeListener="#{workspace.selectAllComponents}">
            <a4j:support event="onclick" reRender="listcomponents"/>
        </h:selectBooleanCheckbox>
    </f:facet>

    <h:selectBooleanCheckbox id="selectComponent" value="#{workspace.selectedComponentIds[componentInfo.id]}" />
</rich:column>

支持 bean:

public void selectAllComponents(ValueChangeEvent event) {
    if (!selectAll) {
        changeMap(selectedComponentIds,true);
        setSelectAll(true);
    } else { 
        changeMap(selectedComponentIds,false);
        setSelectAll(false);
    }
}


public void changeMap(Map<Long,Boolean> selectedComponentMap, Boolean blnValue) {
    if(selectedComponentMap != null) {
        Iterator<Long> itr = selectedComponentMap.keySet().iterator();
        while(itr.hasNext()) {
            selectedComponentMap.put(itr.next(), blnValue);
        }
        setSelectedComponentIds(selectedComponentMap);
    }
}

我在这里找到的答案:

if (event.getPhase() != PhaseId.INVOKE_APPLICATION) {
    event.setPhase(PhaseId.INVOKE_APPLICATON);
    event.queue();
} else {
   //do your stuff here
}

不相加,因为 getPhase()ValueChangeEvent 事件中不可用。我只看到 getPhaseId() 其中包含 INVOKE_APPLICATION 选项,所以我的问题是,如何通过上述答案实现我的功能要求?或者有什么替代方案吗?

I am trying to implement a "select all checkboxes" checkbox in a rich:dataTable header without using JavaScript.

XHTML:

<rich:column styleClass="center-aligned-text">
    <f:facet name="header">
        <h:selectBooleanCheckbox id="selectAll" title="selectAll" valueChangeListener="#{workspace.selectAllComponents}">
            <a4j:support event="onclick" reRender="listcomponents"/>
        </h:selectBooleanCheckbox>
    </f:facet>

    <h:selectBooleanCheckbox id="selectComponent" value="#{workspace.selectedComponentIds[componentInfo.id]}" />
</rich:column>

Backing bean:

public void selectAllComponents(ValueChangeEvent event) {
    if (!selectAll) {
        changeMap(selectedComponentIds,true);
        setSelectAll(true);
    } else { 
        changeMap(selectedComponentIds,false);
        setSelectAll(false);
    }
}


public void changeMap(Map<Long,Boolean> selectedComponentMap, Boolean blnValue) {
    if(selectedComponentMap != null) {
        Iterator<Long> itr = selectedComponentMap.keySet().iterator();
        while(itr.hasNext()) {
            selectedComponentMap.put(itr.next(), blnValue);
        }
        setSelectedComponentIds(selectedComponentMap);
    }
}

An answer which I found here:

if (event.getPhase() != PhaseId.INVOKE_APPLICATION) {
    event.setPhase(PhaseId.INVOKE_APPLICATON);
    event.queue();
} else {
   //do your stuff here
}

doesn't add up, because the getPhase() is not available in the ValueChangeEvent event. I see the just the getPhaseId() which contains the INVOKE_APPLICATION option, so my question is, how can I achieve my functional requirement with the above answer? Or is there any alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2025-01-15 08:07:10

valueChangeListener 在流程验证阶段执行其工作。但是,当提交整个表单时,提交的值将覆盖在更新模型值阶段期间 valueChangeListener 中更改的值。您确实想要在调用操作阶段执行“全选”作业。

更好的方法是让 JSF 跳过 valueChangeListener 方法中的所有剩余阶段,直到渲染响应,这样更改的值就不会被覆盖。您可以通过调用 < 来实现此目的valueChangeListener 方法中的 code>FacesContext#renderResponse()

public void selectAllComponents(ValueChangeEvent event) {
    selectAll = !selectAll;
    changeMap(selectedComponentIds, selectAll);
    FacesContext.getCurrentInstance().renderResponse();
}

The valueChangeListener performs its job during the Process Validations phase. However, as the entire form is submitted, the submitted values will override the in the valueChangeListener changed values during the Update Model Values phase. You indeed want to perform the "Select all" job during the Invoke Action phase.

Better is to just let JSF skip all remaining phases in the valueChangeListener method until the render response, so that the changed values won't be overridden. You can achieve this by calling FacesContext#renderResponse() in the valueChangeListener method.

public void selectAllComponents(ValueChangeEvent event) {
    selectAll = !selectAll;
    changeMap(selectedComponentIds, selectAll);
    FacesContext.getCurrentInstance().renderResponse();
}
淡写薰衣草的香 2025-01-15 08:07:10

我已经通过数据表的标题方面的复选框实现了这一点,但使用了一个简单的jquery,它选择了该表行中的所有复选框。

如果你想避免使用 js,你可以不用刷新表选择,而只需使用全选复选框本身。您可以将其映射到一个 bean,并在您的应用程序中使用 a4j 操作(侦听器)来处理它,您将在其中更新所选的 id 并重新呈现您的表。

还有更多可能可以简化您的选择处理程序:我为实体查询创建了一个通用的可选择包装器,其中包含一个布尔值和实体查询结果的实体。您使用另一个返回这些包装器列表的函数来包装 getresultslist,并且您得到了一些非常通用的东西。在 Eclipse 中,即使通用的自动完成也会为您提供正确的对象。

I have implemented this with a checkbox in the header facet of a datatable but with a simple jquery which selects all checkboxes in this tables rows.

If you want to avoid js and you can life without refreshing the table selection but just use the select all checkbox per se. You can just map it to a bean and treat this in your application with an a4j action(listener) where you will update your selected ids and rerender your table.

There is something more that could probably simplify your selection handler: i've created a generic selectablewrapper for the entityquery which holds a boolean and the entity of the results from the entityquery. You wrap the getresultslist with another function returning a list of these wrappers and you have something very generic. In eclipse even autocompletion of the generic is giving you the correct object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文