Force update of datatables data

Force update of datatables data

WilliamWOCRMWilliamWOCRM Posts: 4Questions: 2Answers: 0
edited February 2017 in Free community support

We are using DataTables with the following libraries:

<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/select/1.2.0/js/dataTables.select.min.js"></script>
<script src="//cdn.datatables.net/rowreorder/1.1.2/js/dataTables.rowReorder.min.js"></script>

And we have come across an issue, sometimes, when we use row reorder to drag and drop rows it will move some other rows to random places, as a fix, we found that using the following in the rowReorder options on the DataTables creation fixed this:

rowReorder: {
        selector: ".moveable",
        dataSrc: "order",
        update: false
},

But this in itself caused an issue, we have a "SaveOrder" function, that uses the table.data() function to get the desired info in the correct order, but because the update has not been fired, it is being sent to our API in the wrong order.

Is there any way to force an update on the data held within table.data in order to pass this in the correct format?

For reference, our SaveOrder looks like this:

jq.ajax({
        url: "index.php?our_api_references",
        type: "POST",
        data: {table_data: send_data},
        beforeSend: function () {
            $("#dataTable").find("tbody").html('<tr><td colspan="100" style="text-align: center">Saving order, please wait. This process can take a few minutes...</td><tr>');
        },
        success: function(data, status) {
            // console.log(data);
        },
        error: function(xhr, desc, err) {
            console.log(xhr);
            console.log("Desc: " + desc + "\nErr: " + err);
        },
        async: true
    });

    window.location.reload();
}

Answers

  • WilliamWOCRMWilliamWOCRM Posts: 4Questions: 2Answers: 0
    edited February 2017

    Edit:
    Function for SaveOrder is actually this, I missed some

    function SaveOrder(){
        var send_data = new Array();
        var added_data = new Array();
        for(i = 0; i < table.data().length; i++){
            
            //send_data.push(table.data()[i]);
            $data = table.data()[i];
            
            $document_id = $data['record_id'];
            $order = $data['order'];
            $parent = $data['parent'];
            
            send_data.push([$document_id, $order, $parent]);
            
        }
    
        jq.ajax({
            url: "index.php?our_api_references",
            type: "POST",
            data: {table_data: send_data},
            beforeSend: function () {
                $("#dataTable").find("tbody").html('<tr><td colspan="100" style="text-align: center">Saving order, please wait. This process can take a few minutes...</td><tr>');
            },
            success: function(data, status) {
                // console.log(data);
            },
            error: function(xhr, desc, err) {
                console.log(xhr);
                console.log("Desc: " + desc + "\nErr: " + err);
            },
            async: true
        });
    
        window.location.reload();
    }
    
  • allanallan Posts: 63,223Questions: 1Answers: 10,416 Site admin

    When it looks "random" is the ordering of the table being performed on any column other than the sequence number? If you can link to a page showing the issue that would be useful.

    Thanks,
    Allan

  • WilliamWOCRMWilliamWOCRM Posts: 4Questions: 2Answers: 0
    edited February 2017

    Hi Allen,
    Unfortunately due to security we cannot provide a link to a page for this, the randomness comes from when the row is moved (e.g. a row at position 20 to position 14), all of a sudden a row at position 5 would move to 12.
    This does not occur on a development system we have for this, but this is the case on a live system (hence the inability to share details of a system).

    I should add that this does not occur all the time, it seems to be sporadic as to when it occurs

  • allanallan Posts: 63,223Questions: 1Answers: 10,416 Site admin

    Are you able to reproduce the error on JSFiddle or http://live.datatables.net so I can attempt to debug it in that case?

    Thanks,
    Allan

This discussion has been closed.