How to add an ID column to a TR if I make a call with ajax?

How to add an ID column to a TR if I make a call with ajax?

HollowPanteraHollowPantera Posts: 6Questions: 2Answers: 0
edited June 2014 in Free community support

Cheers guys this great community of this incredible plugin.

Did you ask them how to add an id column TR making a ajax call to a php file where I make the query to get the records I'm showing?

javascript Code:

<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {

    var table = $("#dataJquery");
    var oTable = table.dataTable({
      "ajax" : "listDatabase.php",
      "language": {
                  "url": "language.json"
              }
    });


    $(document).on('click', '.delete', function() {
        var confirmC = confirm('¿Delete this?');
       if(confirmC){
      var celeb_id = $(this).attr('id').replace('delete-', '');
      var parent = $('#'+celeb_id);
      $.ajax({
        type: "get",
        url: "deleteProd.php?id="+celeb_id,
        data: "",
        success: function() {
            code for refresh table.
            });
        }
      });
    }
    });
  });

The result I want to achieve is as follows:

<tbody>
    <tr id="delete-1">
      <td>1</td>
      <td>Milk</td>
      <td>1.000</td>
    </tr>
    <tr id="delete-2">
      <td>2</td>
      <td>Cheese</td>
      <td>2.000</td>
    </tr>
</tbody>

But as I do so that every column (TR) has a corresponding ID: eg <tr id="delete-1">
making a call with AJAX (?

And my second question is, and reload the page after deleting a record without losing the number of paging where am I?

Thanked all the help they have given me through google.
Greetings from Chile (sorry for my bad English).

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You can use the DT_RowId option (second table in the server-side processing return parameters page - those three parameters will work for client-side processing as well).

    Or use createdRow to add the ID manually.

    Allan

  • HollowPanteraHollowPantera Posts: 6Questions: 2Answers: 0

    Thanks for your reply Allan, proves to me the method you mention.
    I wanted to know if I can answer the second question, on how to refresh the table while maintaining the current number of paging.
    I have tried many methods and none serves me, I have the latest version of all plugins. The code would go to refresh after deleting a record (in success function).
    Waiting for your response or of any user. Greetings from Chile.

  • capitaobrazilcapitaobrazil Posts: 7Questions: 0Answers: 0

    HollowPantera.

    You have to delete this sample and also to edit on another page, sending POST method?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The ajax.reload() method has an option to stay on the current page. If that doesn't work for you, please link to a test case showing the issue.

    Allan

  • HollowPanteraHollowPantera Posts: 6Questions: 2Answers: 0

    Dear Allan, I also used the method you just give me (ajax.reload ()), the console sends me the following error:

    TypeError: oTable.ajax is undefined

    My Javascript (jQuery Code):

        $(document).on("click", ".delete", function() {
            var respuesta = confirm("¿Delete This?");
           if(respuesta){
          var celeb_id = $(this).attr("id").replace("delete-", "");
          var parent = $("#"+celeb_id);
          $.ajax({
            type: "get",
            url: "deleteProd.php?id="+celeb_id,
            data: "",
            success: function() {
                 oTable.ajax.reload();
            }
          });
        }
        });
      });
    

    I wrote the code in success function after deleting a record.
    Waiting for your help. A Greeting.

This discussion has been closed.