Browse table with N lines with FOR and find specific values

Asked

Viewed 973 times

0

How to go through a table with N lines and find the specific values of each Dropdownlist ?

Example of the table below: inserir a descrição da imagem aqui

I tried with the code below, but without success.

  function ValidarStatusGrid() {
        var temp, td;
        var table = $("tbPedidos");
        var status = $("#sclStatusGrid");
        temp = document.getElementById('tbPedidos').getElementsByTagName('tr');
        for (var i = 0; i < temp.length + 1; i++) {
            td += temp[i];
        }
    }
  • 2

    Can’t you put some classes in this dropdown? Then you wouldn’t have to go through the table.

  • @bfavaretto does not think so, depending on the status that is in the dropdown will disable the same or perform another operation. And everything has to be done on the client’s side

  • Well, I think the answer below solves well.

2 answers

2


To walk the lines you can do so:

$('#tbPedidos> tbody  > tr').each(function() {
   // aqui tem a linha (tr)
   var linha = $(this);
});

However, if you specifically want the selects in the table, you can do so:

$('#tbPedidos select').each(function() {
    // aqui tem o valor da cada select
    var valor = $(this).val();
});
  • $('#tbPedidos select') ta taking all selects from the table, if in the future another column also has selects will mix kk information

  • Yes, this fits the question, but it would be better to put a class or name in the select to be able to select correctly, as @bfavaretto commented above

1

I did a jQuery foreach on td from the table, and saved took the value using .val() of each element.

  function ValidarStatusGrid() {
        var table = $("#tbPedidos");
        table.find('tbody tr').each(function() {
            var resultado = $(this).find('td select').find('option:selected').val();
            alert(resultado);
        });
    }
    $('#tbPedidos select').on('change', function () {
        ValidarStatusGrid();
    });
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbPedidos">
      <thead>
      <tr>
        <td>Pedido</td>
        <td>Selecione</td>
      </tr>
      </thead>
      <tbody>
        <tr>
          <td>651981</td>
          <td><select><option value="valor selecionado 01">opção 01</option><option value="valor selecionado 02">opção 02</option></select></td>
        </tr>
      </tbody>
    </table>

Browser other questions tagged

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