How to load a jQuery function when loading a page that uses JSF and Primefaces

2

I'm using a function to check if all the checkboxes are checked to perform the load of an already filled register.

So I wanted to know how to call the function.

function selectAllCheckbox() {

  var booleanArray = new Array();
  var allCheckBox = $('input[id*="ufs"]');

  $(allCheckBox).each(function() {
      if ($(this).prop('checked') == true) {
          booleanArray.push($(this).prop('checked'));                 
      }
  });

  if (booleanArray.length == 27) {
      allCheckBox.last().prop('checked', true);
  }
}

In this I check if all the checkboxes are filled, if so, mark the checkbox all.

<p:selectManyCheckbox id="ufs" value="#{lojaBean.pojo.ufsInss}" layout="grid" columns="4" styleClass="columnLeft">
    <f:selectItems value="#{lojaBean.helper.estados}"/>
    <f:selectItem itemLabel="Todos" itemValue=""/>
    <p:ajax event="change" onstart="selectAll(event)" update="ufs"/> 
</p:selectManyCheckbox>

I use another method selectAll(event) to when selecting the button, select all. I would like when loading a page, it checks if all checkboxes have been filled, but I do not know how to call ...

    
asked by anonymous 31.03.2014 / 19:52

2 answers

1

I was able to do what I needed using the possibility that the system offers me.

When I do this <f:selectItem itemLabel="Todos" itemValue=""/> I review the list an String empty when I select this checkbox .

Before saving I try not to write to the bank this String empty.

private void removeNullValueFromArray(Pojo pojo) {

    List<String> listUf = pojo.getUfsInss();
    listUf.removeAll(Collections.singleton(""));

    pojo.setUfsInss(listUf);
}

When loading the page using the tag,

<f:metadata>
    <f:event listener="#{lojaBean.load}" type="preRenderView"/>
</f:metadata>

I call on Bean method load where I check if the list is filled with the quantity considered full, and then I move to List add String empty.

if (isTodosEstadosSelecionados()) {
    this.getPojo().getUfsInss().add("");                
}

So, it works!

    
01.04.2014 / 16:13
0

Add a script to the file using the tag <h:outputScript> and usually add a listener of type load of jQuery:

$(function() {
     selectAllCheckbox();
})

Another alternative is to simply insert the Javascript inline tag using a <script> tag in your facelet. See some examples here .

However, this will work if the entire page loads. If it is only a partial update with AJAX, use the oncomplete attribute to perform something after the DOM update. Consider the order of PrimeFaces events .

    
31.03.2014 / 20:18