How to delete data from DataTables ?

How to delete data from DataTables ?

adeguntoroadeguntoro Posts: 3Questions: 2Answers: 0

Okay, at this time i have 2K row data or more, i use Datatables because of it's simple but not simple like my mind think.
This is my php code to read data from database as JSON

<?php
require 'db_pdo.php';
header('Content-Type: application/json');
$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);
$kabupaten = $dbh->query("select *from  provinsi inner join kabupaten on kabupaten.id_prov = provinsi.id_prov");

while($row = $kabupaten->fetch()){
    $data_kabupaten[] = $row;
}

print json_encode($data_kabupaten);

and this how i read data from JSOn on DataTables :

                $('#example1').DataTable ({
                    ajax: {
                        url     : "defer_kabupaten.php",
                        dataSrc : ""
                    },
                    columns: [
                        {
                            "data": null, "defaultContent": "", "targets": -1
                        },
                        { data: "prov_nama" },
                        { data: "kab_nama" },
                        { 
                            data: "id_kab", "visible": false,
                            "searchable": false}, 
                        {
                            data: null, className: "center",
                            defaultContent: '<a href="" class="btn btn-warning editor_edit" target="_blank">Edit</a>'
                        }, {
                            data: null, className: "center",
                            defaultContent: '<a href="" class="btn btn-danger editor_remove" target="_blank">Hapus</a>'
                        }
                    ],
                    deferRender: true,
                    responsive: true,
                    lengthMenu: [[5,10, 25, 50, -1], [5,10, 25, 50, "All"]]
                });

And as you can see there's 2 button, edit and delete. I use this to make the button can read simple string from my table as my id, but when i use .val() it doesn't work.

$('#example1').on('click', 'a.editor_remove', function (e) {
    e.preventDefault();
    var currentRow  = $(this).closest("tr");
    var col2        = currentRow.find("td:eq(2)").html();
    alert(typeof(col2));
}):

So, how i can delete data from Datatables ?

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @adeguntoro ,

    The simplest way would be to use Editor, this has CRUD capabilities but is licensable.

    If you want to do it with without Editor, you'll need to send that row information back via Ajax to your server-side script which would then edit/remove from the database itself. This thread here might help.

    Cheers,

    Colin

This discussion has been closed.