mehere
mehere

Reputation: 1556

check f:items value based on p:selectManyCheckbox using javascript

I would like to know how to check f:items value based on p:selectManyCheckbox using javascript.

What I did is Xhtml

<p:selectBooleanCheckbox itemLabel="Select All" onchange="changeSelectAllStatus(this);"
    value="#{myBean.selectAllStatus}"
    id="selectAllStatus" style="margin-left:4px;" >
</p:selectBooleanCheckbox>

<p:selectManyCheckbox onchange="changeServiceStatus(this);"
    styleClass="servicestatusstyle"
    value="#{myBean.serviceStatusFilterList}"
    layout="pageDirection" id="empListSub">
    <f:param name="tabValue" value="1" />
    <f:selectItems value="#{myBean.serviceStatusList}" var="status"
            itemLabel="#{status.title}" itemValue="#{status.id}"/>
</p:selectManyCheckbox>

The generated html is

enter image description here

Javascript

function changeSelectAllStatus(element)
    {
         var selectAll = element.checked;
         var serviceStatusChildNodes = document.getElementById("accord:empListSub").getElementsByTagName('input');
         for ( var i in serviceStatusChildNodes ) {
             if ( serviceStatusChildNodes[i].type=='checkbox' ) {
                 serviceStatusChildNodes[i].checked = selectAll;
             }
         }

        if(selectAll)
        {
            selectAllStatusArray = selectAll;

        }
        else
        {
            selectAllStatusArray = selectAll;

        }
    }

My problen is when 'select all' is checked,the f:items checked values are being set as true but in user interface it is not being checked. Can anybody point out where have i gone wrong?

Upvotes: 2

Views: 3699

Answers (1)

Hatem Alimam
Hatem Alimam

Reputation: 10048

Simply you can do this:

<p:selectBooleanCheckbox itemLabel="Check All" 
                         widgetVar="checkAllWV"
                         onchange="selectAllCheckBoxes(PF('manyCheckWV'))">
</p:selectBooleanCheckbox>

<p:selectManyCheckbox widgetVar="manyCheckWV">                                                    
   <f:selectItems value="#{mainBean.list}"/>
</p:selectManyCheckbox>

The onchange of the booleanCheckbox would control the manyCheckbox values.

Now the change event

function selectAllCheckBoxes(manyCheckBoxes) {
   manyCheckBoxes.inputs.each(function() {
      if(PF('checkAllWV').isChecked()) {
         $(this).prop('checked', false)
         PF('checkAllWV').jq.find('.ui-chkbox-label').text('Uncheck all')
      } else {
         $(this).prop('checked', true)
         PF('checkAllWV').jq.find('.ui-chkbox-label').text('Check all')
      }
      $(this).trigger('click');
   })
}

You can find a small example on github.

Upvotes: 4

Related Questions