How to take individual td values from dynamic table without clicking on the row

Asked

Viewed 282 times

0

I have a button that sends the form, and inside the form have the respective fields, inside the fomulário has a table that brings dinanmicamente the data of the database that is allows editing of the values of each line: tabela

    $("#update-baixa").click(function () {

        var item = $("[data-idparcela]").closest("tr").children('td:eq(1)');

        console.log(item.val());

    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tablemdlreceita" class="table table-sm table-bordered table-responsive">
    <thead>
        <tr>
            <th>Documento</th>
            <th>Vlr Programado</th>
            <th>Dt Vencto</th>
            <th colspan="2" class="text-center">Ações</th>
        </tr>
    </thead>
    <tbody><tr>
            <td>FR212</td>
            <td>220.00</td>
            <td>30/01/2019</td>
            <td align="center">
                <a class="omdleditreceita" data-idparcela="3">Editar</a>
            </td>
        </tr>
        <tr>
            <td>FR212</td>
            <td>400.00</td>
            <td>25/01/2019</td>
            <td align="center">
                <a class="omdleditreceita" data-idparcela="4">Editar</a>
            </td>
        </tr>
    </tbody><tbody>
        <tr>
            <td colspan="9" align="center">
                <div class="pagination-wrap">
                    <ul class="pagination"><li><a href="/add-data.php?page_no=1" style="color:red;">1</a></li></ul>                                                
                </div>
            </td>
        </tr>
    </tbody>
</table>

                        <button type="button" id="update-baixa" class="btn btn-primary"><span class="fas fa-plus"></span>Salvar</button>

Only it is not taking the individual value of each td of the column |Vlr Programmed|

  • 1

    Change: Closest to parents and val() to text().

  • Sure he did, but he’s taking the value of line two only. I need it to take the value of line 1 so I can store in a variable and after line two and do the same.

  • He’s only picking up one line because it’s set the td:eq(1). Remove the eq and treat the return as a array.

1 answer

1


I don’t know if the way your code is implemented, it will perform well, but if you want you can take the values of each td just setting your position from the attributes date.

$("#update-baixa").click(function() {

  var tam = $("[data-idparcela]").length;

  for (var i = 0; i < tam; i++) {
    for(var j = 1; j<tam; j++) {
      var item = $("[data-idparcela]:eq("+i+")").parents("tr").children("td:eq("+j+")");

      console.log(item.text());

     }
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tablemdlreceita" class="table table-sm table-bordered table-responsive">
  <thead>
    <tr>
      <th>Documento</th>
      <th>Vlr Programado</th>
      <th>Dt Vencto</th>
      <th colspan="2" class="text-center">Ações</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>FR212</td>
      <td>220.00</td>
      <td>30/01/2019</td>
      <td align="center">
        <a class="omdleditreceita" data-idparcela="3">Editar</a>
      </td>
    </tr>
    <tr>
      <td>FR212</td>
      <td>400.00</td>
      <td>25/01/2019</td>
      <td align="center">
        <a class="omdleditreceita" data-idparcela="4">Editar</a>
      </td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td colspan="9" align="center">
        <div class="pagination-wrap">
          <ul class="pagination">
            <li><a href="/add-data.php?page_no=1" style="color:red;">1</a></li>
          </ul>
        </div>
      </td>
    </tr>
  </tbody>
</table>

<button type="button" id="update-baixa" class="btn btn-primary"><span class="fas fa-plus"></span>Salvar</button>

  • What would be a way for you to perform better? I hadn’t used those JS selectors yet.

  • It is that I do not know what will be the size of the table, because as you will be dynamic, this way the code is going through many times the DOM, can have some performance problem, depending on as said, the size of the table.

  • I also don’t know for sure the size, it’s dynamic. else this way worked out, to make a go and get all the values where eq[i] because for example if I have more than two lines it already takes everything, it’s like that’s my code?

  • I edited the answer using a for.

  • Thank you very much, that’s just what I need.

Browser other questions tagged

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