Home » Jquery » javascript – Datatable body become blank after sorting or serching problem-Exceptionshub

javascript – Datatable body become blank after sorting or serching problem-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

After building table body using .html() function by jQuery ajax call, sorting searching and export not working, make table blank.

var id = $("#userid").val();

$.ajax({
    type: "POST",
    url: "services/service.php",
    data: {
        cmd: 'get_users',
        id: id
    },
    cache: false,
    dataType: 'json',
    success: function (res) {
        var html = "";
        l = res.length;
        for (var i = 0; i < l; i++) {
            html += "<tr><td>" + res[i].id + "</td><td>" + res[i].username + "</td><td>" + res[i].name + "</td><td>" + res[i].role + "</td><td>" + res[i].parent + "</td><td>" + res[i].address + "</td><td>" + res[i].email + "</td><td>" + res[i].mobile_no + "</td><td><button class='uedit'>Edit</button></td><td><button>Delete</button></td></tr>";
        }
        $('#example tbody').html(html);
    }
})
How to&Answer:

From this code it seems that table will build properly with user’s data when you call this ajax. But what you do while sorting, searching and export, it is important. Check the response of ajax for sorting, searching and export. May be response is empty so empty response makes response.length = 0 and html will be empty and it is overwrite to $('#example tbody') with empty html. html() replaces old elements with new elements.

All this is based on assumption that you get data from server on every sorting, searching and export request. If it is not then show me the code of sorting, searching and export.