My Datatables freezes when no results from ajax url

My Datatables freezes when no results from ajax url

kabezakabeza Posts: 14Questions: 5Answers: 0

I've a table which also has an external select used to filter the table with an ajax parameter

$("#seldepartamento").change(function(event) {
    myTable.draw();
});

var myTable = $('#myTable').DataTable({
    "processing": true,
    "serverSide": true,
    "serverMethod": "post",
    "ajax": {
        "url": '/datos_json',
        data: function(data) {
            data.param1 = $('#seldepartamento').val();
        }
    },
    "bFilter": true,
    "ordering": true,
    "order": [[ 1, "asc" ]],
    "deferRender": true,
    "responsive": true,
    "lengthMenu": [ [10, 50, 100, -1], [10, 50, 100, "Todos"] ],
    "pageLength": 50,
    "sPaginationType": "full_numbers",
    "bPaginate": true
});

All works fine except the table shows "Processing" in case my selection (external filter) or search matches 0 records... I have to change option or empty the table's search field for it to work fine again. The table should show "No data available in this table" or something similar... The json returned (when there are no matches in db) is

{"draw":1,"recordsTotal":0,"recordsFiltered":0,"data":"[]"}

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947
    Answer ✓

    I believe your JSON should look like this:
    {"draw":1,"recordsTotal":0,"recordsFiltered":0,"data":[]}

    Notice no quotes around the [] in the data object.

    Kevin

  • kabezakabeza Posts: 14Questions: 5Answers: 0

    Hi Kevin
    That was the trick!

    Thanks!

This discussion has been closed.