checkboxjsf-1.2

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.

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.

My code was written in 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. Here is the JSF code,

<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>

Solution

  • Possible solution is to use h:inputHidden component (I think it exists in JSF 1.2. If not, you can find some alternative).

    For example

    1. Add h:inputHidden to the page

      <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. 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

      <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

    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.