Datatable reorder and save to database

Datatable reorder and save to database

GeorgeGeorgievGeorgeGeorgiev Posts: 2Questions: 1Answers: 1

Hi guys, I'm struggling to make the Datatable to save to database i have include the reorder extensions but nothing happen i was thinking that the properties will be in the reorder extension but I have mistaken. Here is my code which turn rowReorder extension on :

$(document).ready(function () {
var table = $('#dattab').DataTable({rowReorder: true,
dom: 'Bfrtip',
buttons: [
'copyHtml5', 'excelHtml5', 'csvHtml5','pdfHtml5'
]
});
});

  1. The extension definitely is on because i can move the elements but when i drop them in the desirable order they do not persist that way. What need to import here in the code above;
  2. How to trow the ajax request with the new order?.
  3. What is the algorithm or sample of code to save in database. I'm using php.

I have search over the last night for clear example but i have not find nothing.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,258Questions: 1Answers: 10,421 Site admin

    Could you link to the page showing the issue so we can see what is happening please.

    Allan

  • GeorgeGeorgievGeorgeGeorgiev Posts: 2Questions: 1Answers: 1
    Answer ✓

    Hi, Allan,

    i have made it to work ... It was my error to use older php. I will share what was my solution:

    First the initialization code:

       var table = $('#dattab').DataTable({
                rowReorder: {
                    selector: 'tr',
                    //   update: true,
                    //   dataSrc: '.ord-id'
                },
                dom: 'Bfrtip',
                buttons: [
                    'copyHtml5', 'excelHtml5', 'csvHtml5', 'pdfHtml5'
                ]
            });
    

    Second i was needed to attach event on which to send the ajax request i have try with the reorder but it not helped me for some reason it was sending one redraw old on the table so i use the draw event and it sends the ajax when the table is redrawn:

     $('#dattab').on('draw.dt', function () {
                if ($('#dattab').data('tabs')) {
                    var rows = table.rows().data();
                    var ord = new Array();
                    for (var i = 0, ien = rows.length; i < ien; i++) {
                        ord[i] = rows[i].DT_RowId;
                    }
                    post_order(ord, $('#dattab').data('tabs'));
                }
            });
    

    Third i need a php script to handle the reorder:

    if( $_POST['elem'] && $_POST['table']) {
        $i = 1;
        $error = 0;
        foreach ($_POST['elem'] as $row) {
            $q = "UPDATE " . "fit_" . $_POST["table"] . " set
                `order` = ".$i.  "
                ,updated_at = Now()
                WHERE id = " . GetSQLValueString($row, "int");
    
            $res = $mysqli->query($q);
            $i++;
            if(!$res)
            {
                $error++;
            }
    
        }
        if(!$error)
        {
            echo 'success';
        }
        else{
            echo 'error';
        }
    

    What do you think?

  • allanallan Posts: 63,258Questions: 1Answers: 10,421 Site admin

    Looks good - thanks for sharing your solution with us.

    Allan

This discussion has been closed.