0

I'm trying to create a delete button in my DataTable which deletes data from a database.

I have created the icon button, but I don't know how to insert a 'delete' function in to the DataTable.

I think I have to use an AJAX request to call PHP to delete the row in the DataTable. I have tried some stuff, but nothing works.

$("#tab").dataTable({
  serverSide: false,
  ajax: {
    type: "GET",
    url: plugin_ajax_object.ajax_url,
    dataSrc: "", // Aller sur un fichier php pour aovir une url
  },
  columns: [
    { data: "david_enable", },
    { data: "david_nom", },
    { data: "david_url", },
    { data: "david_cms", },
    {
      data: "david_site_id",
      render: function(data, type, row) {
        return `<a href="admin.php?page=enable.php${data}" class="dashicons-before dashicons-edit">Activer</a><br>
          <a class="dashicons-before dashicons-trash" id="Delete">Supprimer</a><br>`
      }
    }
  ],
  createdRow: function(row, data, dataIndex) {
    if (data["david_enable"] == null) {
      $(row).addClass('bg-danger');
    }
  },
  createdRow: function(row, data, dataIndex) {},
  language: {
    lengthMenu: "Afficher _MENU_ éléments",
    search: "Rechercher :",
    info: "Affichage de l'élément _START_ à _END_ sur _TOTAL_ éléments",
    paginate: {
      first: "Premier",
      last: "Dernier",
      next: "Suivant",
      previous: "Précédent",
    },
    infoEmpty: "",
    emptyTable: "",
    zeroRecords: "",
    loadingRecords: "Chargement...",
    processing: "En cours...",
  },
  pagingType: "simple_numbers",
  lengthMenu: [10, 20, 30],
  pageLength: 10,
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Xora
  • 13
  • 3
  • try onclick method and call delete function...like dis onClick="deleteId(1)" – pk_ Feb 28 '22 at 09:52
  • The part your missing from your question is how you provide the click handler for the delete *buttons*. In your `render` function add a class to your `a` `Supprimer` then use *event delegation* to handle the click (don't use `onclick=`), eg `$(document).on("click", ".delete_button", function() { var btn = $(this); ... });` – freedomn-m Feb 28 '22 at 10:09
  • It's unclear if your issue is how to create a "button" (you already have a `render:` function, so that should be ok) or how to handle the button click - if you've tried `$("#Delete").click...` then it won't work because a) IDs *must be unique* so `$("#Delete")` will only pick up the first and b) the buttons are create dynamically, so you need *event delegation* - if it's the (b) case then see: [event binding on dynamically created elements](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – freedomn-m Feb 28 '22 at 10:11
  • Hey! Thanks for you answer. I know how to create the button , but i don't know how to delete the row in the bdd with this button. My friend told me i have to do a ajax request, but i don't know how to do it – Xora Feb 28 '22 at 10:39

0 Answers0