checkbox "select all" jquery and CakePHP

3

Hi, I have foreach in a table that lists several checkboxes . I need to do a jquery function that by clicking on the checkbox of <th> % with_% the checkboxes of the next selecione todos . Look at my code:

foreach ($controllersActions as $acaoControlador) {
   if ($controlador != $controladorAtual) {
                            $controlador = $controladorAtual;
                            ?>
                        <tr>
                            <th colspan="2">
                                <?php 
                                echo $this->Form->checkbox('all', [
                                    'hiddenField' => false,
                                    'class' => 'all',

                                ]);
                                ?>
                                <?= Inflector::humanize($controladorAtual) ?>
                            </th>   
                        </tr>
                        <?php
                        }
                        ?>
                        <tr>
                            <td style="padding-left: 20px; width: 10px;">
                                <?=
                                $this->Form->checkbox('permissoes[]', [
                                    'hiddenField' => false,
                                    'value' => $acaoControlador,
                                    marcado($entidade, $acaoControlador)
                                ]);
                                ?>
                            </td>
                            <td><?= Inflector::humanize($acaoAtual); ?></td>
                        </tr>
                    <?php }; ?>

Above is the list of my checkboxes

Below is my jquery function:

 $('.all').on('click', function(e){
            $this = this;

            $.each($(this).children('tr td').find('checkbox'), function(i, item){
                alert('oi');
              $(item).prop('checked', $this.checked);
            });

          });
      <?= Inflector::humanize($controladorAtual) ?>
         </th>  
  </tr>

An image of the checks on the screen.

Hereistherenderedhtml.

Butclickingthecheckboxthattriggersthisfunction,nothinghappens.Iremovedthejquerycodefromthisexample: link

Can anyone help me?

    
asked by anonymous 14.01.2016 / 12:21

1 answer

1

First, you must have something to identify the checkbox that will be selected, can be a class in common, can be within a div or any other block. Assign the event change to checkbox which will be responsible for selecting all and change the behavior of change to mark / unmark.

The main item is to have some way to identify this checkbox group, see the example with the checkboxes inside a div.

$('body').on('change', '.all', function() {
  $(this).next('div').find('input[type=checkbox]').prop('checked', this.checked); //jQuery <= 1.6 usar attr
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" class="all" />
<div id="minhaDiv">
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
</div>
<input type="checkbox" class="all" />
<div id="minhaOutraDiv">
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
  <input type="checkbox" />
</div>
    
14.01.2016 / 12:57