0

I am working with a invoice form, and I don't have a clue in how can I generate a id in each td, when I try to edit the invoice form... Because I use ajax to call the different data of each product and when I need to change some product, per example in the second or third "td" always change the first "td"...

part of the invoice form:

<td><input type="text" name="cat[]" id="cat_(HERE I NEED PUT A ID)" class="form-control"></td>
<td><input type="text" name="subcat[]" id="subcat_(HERE I NEED PUT A ID)" class="form-control"></td>
<td><input type="text" name="vineta[]" id="vineta_(HERE I NEED PUT A ID)" class="form-control"></td>
user3236149
  • 167
  • 4
  • 16

2 Answers2

0

You almost certainly don't want an id on every td element, but if you really do, here's one way:

// Spin through all rows in the table
$("#yourtable tr").each(function(row) {
    // Spin through all cells in the row
    $(this).find("td").each(function(col) {
        // Assign an ID
        this.id = "cell" + row + "x" + col;
    });
});

...where #yourtable is just a placeholder for a selector that matches your table. That assumes you don't have any nested tables.

Again, though, you probably don't want that. You haven't given any details on what's triggering the ajax call, but usually when this sort of thing comes up, it's in relation to a user action on an element in the row where you want the change made (for instance, increasing or reducing a count). If that's your use case, remember the row, and then update the cell within that row rather than doing a search on the document as a whole.

Example:

$("table").on("click", "input[type=button]", function() {
  var $btn = $(this),
      operation = $btn.val() == "+" ? 1 : -1,
      $row = $btn.closest('tr');
  
  // Simulating ajax by using setTimeout...
  setTimeout(function() {
    var $input = $row.find('input[name="foo[]"]');
    $input.val(+$input.val() + operation);
  }, 10);
});
<table>
  <tbody>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
    <tr>
      <td><input type="text" name="foo[]" value="0"></td>
      <td><input type="button" value="+"><input type="button" value="-"></td>
    </tr>
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Let me explain better, when I refresh the page or when I go to the edit.php page, the invoice form appears with the products I want to edit, I need that between the TDs( or inside of the input of that TD) of each product appears an ID (_1,_2,_3,...) for each TD (per example ``), and so I can edit it thru AJAX the product I want or need to edit. This is like the invoice I am have, look in this url: [invoice form](http://demo.smarttutorials.net/invoice-generator-jquery-autocomplete/) – user3236149 Apr 18 '15 at 19:21
0

Well finally I found the answer following this post answer for xdazz

The code:

$sql = "SELECT @i := @i + 1 AS i, cod, nombreProd, .....FROM PRODUCTS, (SELECT @i:=0) b WHERE .... ";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $i = $row['i'];
$cod = $row['cod'];
....

And inside of each tr > td > input:

<td><input type="text" data-type="cod" name="cod[]" value="<?php echo $cod; ?>" id='cod_<?php echo $i; ?>' minlength="2" class="form-control autocomplete_txt required" autocomplete="off" required></td>
Community
  • 1
  • 1
user3236149
  • 167
  • 4
  • 16