How to mark/deselect checkboxes per column using Jquery

Asked

Viewed 634 times

3

Here at the company we are doing a plant management. The apartments/units that have equal end (for example, Apto 1, Apto 11, Apto 21) and so on, are below each other in the construction and for this reason the plant linked to them is the same, but if a client asks for customization in his unit, the standard plan of the others needs to be the same of customization. We could just use one UPDATE in the table but I am doing as I was asked and as we were asked by the builders until because it may be that the customization is done only in the unit purchased by the customer and according to what he ordered but if the company decides to change all the plants so that the the pattern is the same as the custom pattern, We need to select all of that column to link. Each building contains a different amount of units per floor, some contain 4, others 5 and so on, so the amount of columns returned in the table may vary. At the moment, we are only using a function to select all the units or no unit but I wanted to know, how can I modify my function to select or deselect the units of only one column.

The function is

<script>
    function marcardesmarcar(status){
     $(".chk").each(
            function(){
              if ($(".chk").prop("checked")) 
              $(this).prop("checked", status);
              else $(this).prop("checked", status);               
            }
       );
    }

           </script>'

And the method to show the checkboxes for each unit is

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if ($nome_torre_old != $row['nome_torre'] OR 
            $nome_bloco_old != $row['nome_bloco'] OR
            $andar_old      != $row['andar']) {
            echo '</tr><tr>';
            $nome_torre_old = $row['nome_torre'];
            $nome_bloco_old = $row['nome_bloco'];
            $andar_old      = $row['andar'];
        }
        echo '<td>      
            <input class="chk" style="margin-left:20px;" type="checkbox" id="p'.$row['id'].'" name="p'.$row['id'].'" ';
        if ($row['plt_uni_in_id_unidade']== $row['id'] AND $row['plt_uni_in_id_planta'] == $id_work) echo 'checked="checked"';
        echo ' />'.$row['nome_torre'].' '.$row['nome_bloco'].' '.$row['nome_apto'].'</td>';
    }

Here the image of how it looks, in this case, the building has 4 units per floor and shows 4 columns.

inserir a descrição da imagem aqui

So I was wondering if you have any way to enter the check/uncheck option in the header of each column, to be able to check/uncheck the options in it. Of the ways I thought, I need to know in advance how many columns will be displayed in the table but as I said, each building has a different amount of units per floor so the amount of columns will vary. Sorry for the huge question but I wanted to explain some things to avoid answers that won’t suit me.

  • What you want is when selecting, select only everyone from a tower?

  • I understood according to André Baill’s question. I think he just wants to select everyone from a certain block. What makes it confusing is the introduction regarding the business model. Avoid posting something very specific. Post something generic with fictitious data, even for privacy. Just post this last text that is after the image.

  • The apartment 1 is below the 11 that is under the 21 and so it goes This means that the plan of all Apto 1, 11, 21, 31 of the South and North Tower will be equal. But when a guy goes in there and buys a plant unit and says, "Look, I want you to take a room and do an office." the company can change only the plan of the unit that the customer bought or that of all the units that are above and below and the units of the same one of the other block/ tower will be equal because it has to follow standard.

  • Like, just look at the image I posted from the table and tell me how to select only by column, and the number of columns can vary according to the number of units per floor, that is I have no way of knowing if the table will have 2/3/4/5/6 columns but I need to select BY column.

2 answers

2

Would that be?

$(function(){
    $("table tr").each(function(){
        $(this).find("td").eq(<aqui passa o indice da coluna>).find("input").attr("checked", "checked");
    }); 
});

$(function(){
    $("table tr").each(function(){
        $(this).find("td").eq(1).find("input").attr("checked", "checked");
    }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Coluna 1</td>
    <td>Coluna 2</td>
    <td>Coluna 3</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
    <td>
      <input type="checkbox">
    </td>
  </tr>
</table>

0

There are several possible solutions. Here’s one of them using the data you left in the code

<?php
$cols = 4; // Quantidade de unidades por bloco ou por andar

$rows = array();
// Gerando dados ficticios
$id = 1;
for ( $andar = 1; $andar < 6; $andar++) { // Torre Norte
    for ( $unidade = ($andar * 100) + 1; $unidade < ($andar * 100) + 5; $unidade ++) {
        $rows[] = array('id' => $id++, 'nome_torre' => 'Torre Norte', 'nome_bloco' => '', 'nome_apto' => 'Apto ' . $unidade, 'andar' => $andar);        
    }
}
for ( $andar = 1; $andar < 6; $andar++) { // Torre Sul
    for ( $unidade = ($andar * 100) + 1; $unidade < ($andar * 100) + 5; $unidade ++) {
        $rows[] = array('id' => $id++, 'nome_torre' => 'Torre Sul', 'nome_bloco' => '', 'nome_apto' => 'Apto ' . $unidade, 'andar' => $andar);      
    }
}

echo '<table><tr>';
for ($i = 0; $i < $cols; $i++ ) {
    echo sprintf('<th><input type="checkbox" id="col%d" onclick="toogleUnidades(\'col%1$d\');"/>Selecionar Unidades Final %d</th>', $i, $i+1);
}
echo '<tr></tr>';
// O código abaixo só existe no exemplo
// Remover - Inicio 
$row = reset($rows);
            $nome_torre_old = $row['nome_torre'];
            $nome_bloco_old = $row['nome_bloco'];
            $andar_old      = $row['andar'];
// Remover - Fim

// A linha abaixo no código final será for ($i = 1; $row = mysql_fetch_assoc($result); $i++ ) {    
for ($i = 0; $row = array_shift($rows); $i++ ) {
        if ($nome_torre_old != $row['nome_torre'] OR 
            $nome_bloco_old != $row['nome_bloco'] OR
            $andar_old      != $row['andar']) {
            echo '</tr><tr>';
            $nome_torre_old = $row['nome_torre'];
            $nome_bloco_old = $row['nome_bloco'];
            $andar_old      = $row['andar'];
        }
        $class = 'col' . ($i % $cols);
        echo sprintf('<td><input class="chk %s" style="margin-left:20px;" type="checkbox" id="p%d" name="p%2$d" %s />%s %s %s</td>', 
            $class, 
            $row['id'], 
//          ($row['plt_uni_in_id_unidade']== $row['id'] AND $row['plt_uni_in_id_planta'] == $id_work ? 'checked="checked"' : ''),
            '',
            $row['nome_torre'],
            $row['nome_bloco'],
            $row['nome_apto']
            );
}
echo '</tr></table>';           
?>
<script type="text/javascript">
    function toogleUnidades(id) {
        var st = $("#" + id).is(":checked");        
        $("." + id).each(function(){
            this.checked = st;
        });
    }

</script>

Explaining a few excerpts:

  • In sprintf I used %2$d notations which indicates that it should be replaced by the second argument from the list of places as a decimal
  • In javascript I created a checkbox for the column with an id and added the class with the same id name to the elements of this column, so I can use the selection of all elements with the same class name

Browser other questions tagged

You are not signed in. Login or sign up in order to post.