Get table row values

Asked

Viewed 3,805 times

4

I need to get the values of each row of my table. My table has no fixed number of rows or columns.

I only desire the values of inputs (within the td). Desired inputs have class quantidadeDigitada

inserir a descrição da imagem aqui

The ideal would be to create an array of values per line. For example:

Valores[0] = 20.40;
Valores[1] = 20.40;
Valores[2] = 20.40;

Because I need to get the data as follows:

Valores.toString() = 20.40,20.40,20.40;

With this I can identify the quantity per product based on the position of the array

1 answer

5


  1. First select your inputs:

    $('.quantidadeDigitada')
    
  2. Then pick up your lines:

    .closest("tr")
    
  3. Then mark each line at a value:

    .map(function() {
    
  4. Return the value of inputs formatted as you want:

        return $(this).find("input:eq(0)").val() + "." +
               $(this).find("input:eq(1)").val();
    })
    
  5. The result will be an array where each element is a line. Finally, join them with the comma:

    .toArray()
    .join(',')
    

Full example:

alert(
    $('.quantidadeDigitada')
        .closest("tr")
        .map(function() {
            return $(this).find("input:eq(0)").val() + "." +
                   $(this).find("input:eq(1)").val();
        })
        .toArray()
        .join(',')
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tr>
    <td><input class="quantidadeDigitada" value="10"></td>
    <td><input class="quantidadeDigitada" value="20"></td>
  </tr>
  <tr>
    <td><input class="quantidadeDigitada" value="30"></td>
    <td><input class="quantidadeDigitada" value="40"></td>
  </tr>
  <tr>
    <td><input class="quantidadeDigitada" value="50"></td>
    <td><input class="quantidadeDigitada" value="60"></td>
  </tr>
</table>

  • The only problem is that my table has relative columns because it is generated dynamically portando can not make a note to .find("input:eq(0)"). I would need to traverse a line and add to each instance of the array the values of that line separated by the character "."

  • @Rafaelbarbosa So it’s N columns per row, right? It would work a $(this).find("input").map(function() { return this.value }).toArray().join('.')? (the same strategy used for rows, repeated for columns)

  • 1

    It worked as follows: $('.quantityDigited') . Closest("tr") . map(Function () { Return $(this).find(".quantityDigited").map(Function () { Return this.value }).toArray().Join('.') }) . toArray() . Join(',') );

Browser other questions tagged

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