简体   繁体   English

需要获取所选复选框的动态计数

[英]Need to get dynamic count of the selected check-boxes

I'm having the two types of check-boxes one is for selectAll check-box in the data table header, and another type selecting the check-box for each row. 我有两种类型的复选框,一种是数据表标题中的selectAll复选框,另一种是选择每一行的复选框。

I'm doing a operation, So I need to show the confirmation message, How do I get the count of the selected check-boxes from the Managed Bean. 我正在做一个操作,因此我需要显示确认消息,如何从托管Bean中获取所选复选框的计数。

My code was written in JSF 1.2. 我的代码是用JSF 1.2编写的。

I can able to do select all records, select records, ManagedBean is working fine, But I need to get how many of them got selected for deletion. 我可以选择所有记录,选择记录,ManagedBean工作正常,但是我需要选择其中的几个进行删除。 Here is the JSF code, 这是JSF代码,

<i:commandLink id="delete" 
    onclick="if (!confirm('#{managedBean.deleteSelectedCount}')) return false;"
    action="#{managedBean.deleteRecords}"
    title="Delete records"
    immediate="true">
    <i:graphicImage url="images/icons/delete.gif" alt="Delete records" />
</i:commandLink>
 ;
 ;//Some coding
 ;
 //Data table code starts
 <i:dataTable id="caseDataTable"


 <h:column>
    <f:facet name="header">                             
        <i:selectBooleanCheckbox id="selectAllRecords" title="select All records" 
            value="#{managedBean.selectAll}">
                <a4j:support event="onclick"  reRender="caseDataTable,globalMessages" action="#{managedBean.actionSelectAllRecordss}"                                                           onsubmit="showBusyIndicator();" oncomplete="hideBusyIndicator();" />
        </i:selectBooleanCheckbox>                          
    </f:facet>
    <h:outputLabel for="selectCheckbox" value="selectCheckbox"/>
        <i:selectBooleanCheckbox id="selectCheckbox" 
            title="select a record" value="#{managedBean.selected}" >
                <a4j:support event="onclick" reRender="selectAllRecords, globalMessages" action="#{managedBean.actionSelectionChange}"
                  onsubmit="showBusyIndicator();"  oncomplete="hideBusyIndicator();"/>
        </i:selectBooleanCheckbox>
</h:column>

Possible solution is to use h:inputHidden component (I think it exists in JSF 1.2. If not, you can find some alternative). 可能的解决方案是使用h:inputHidden组件(我认为它存在于JSF 1.2中。如果没有,则可以找到一些替代方法)。

For example 例如

  1. Add h:inputHidden to the page h:inputHidden添加到页面

     <h:inputHidden id="selectedCountHidden" value="#{managedBean.deleteSelectedCount}"/> 
  2. Each time you click on header check box or any of row check boxes , calculate deleteSelectedCount value and re-render h:inputHidden . 每次单击标题复选框或任何行复选框时deleteSelectedCount计算deleteSelectedCount值并重新呈现h:inputHidden Something like 就像是

     <i:selectBooleanCheckbox id="selectCheckbox" title="select a record" value="#{managedBean.selected}" > <a4j:support event="onclick" reRender="...,selectedCountHidden,..." 
  3. And now, since h:inputHidden will always hold deleteSeletedCount value, you can read its value via java script so there is no need for re-loading the page when you click on commandLink 现在,由于h:inputHidden将始终保留deleteSeletedCount值,因此您可以通过Java脚本读取其值,因此在单击commandLink时无需重新加载页面

     <i:commandLink id="delete" onclick="if(!confirm(document.getElementById('form:selectedCountHidden').value))return false;"..../> 

Note that if you have form with id defined, you will need to call 请注意,如果您有定义了id表单,则需要调用

document.getElementById('form:selectedCountHidden').value

Otherwise just call 否则就打电话

 document.getElementById('selectedCountHidden').value

In any case, inspect page source and you will find the exact id of p:inputHidden . 无论如何,请检查页面源,您将找到p:inputHidden的确切id

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM