3

I am able to populate data in my jQuery DataTable using Ajax. But after this, search, sort and pagination of jQuery DataTables has stopped working. Please help.

Here is my HTML code :

<table id="account-details-result-table"
    class="table table-bordered text-center css-fonts-calibri">
    <thead>
        <tr>
            <th>Organization Id</th>
            <th>Organization Name</th>
            <th>Parent OpCo Name</th>
            <th>Registered Email Id</th>
            <th>Registered Phone Number</th>
        </tr>
    </thead>
    <tbody id="search-results-table-tbody">
        <!-- append data here -->
    </tbody>
</table>

Below is the function to initialize jQuery DataTable of Search Results. I' calling it inside $(document).ready() :

function initResultDataTable(){
    $('#account-details-result-table').DataTable({
                        "order": [],
                        "columnDefs": [ {
                        "targets"  : 'no-sort',
                        "orderable": false,
                        }]
                });
}

And here is my ajax call :

function sendSearchAccountDetailsRequest(orgQueryReqJSONString){

    $.ajax({
            type : 'POST',
            url : ctx+'/SearchController',
            data: orgQueryReqJSONString,
            contentType: 'application/json',                                                                            
            success : function(response) {                   
            //process JSON response here                
            var counter = 0;
            var tableDataHTML = '';

            $.each(response.organizationDetailsList, function(counter){
                var $curr = response.organizationDetailsList[counter].organizationDetails;
                tableDataHTML += '<tr id="searched-row-'+counter+'" class="js-result-tbl-tbody-tr">'+
                                 '<td>'+$curr.organizationID+'</td>'+
                                 '<td>'+$curr.organizationName+'</td>'+
                                 '<td>'+$curr.parentOpCoName+'</td>'+
                                 '<td>'+$curr.registeredEmailID+'</td>'+
                                 '<td>'+$curr.registeredPhoneNo+'</td>'+
                                 '</tr>';               
                });

            $('#search-results-table-tbody').empty();
            $('#search-results-table-tbody').append(tableDataHTML);
            },
            error : function(response) {                 
            //handle errors here
            alert('Error !!!'+response);            
            }
    }); 
}
5
  • Datatables has its own AJAX option. Commented Jul 17, 2017 at 13:41
  • @Shiffty, I am aware about that part. But, I have to follow above mentioned approach only. Commented Jul 17, 2017 at 13:45
  • I'm assuming that you're doing it like this so you can load server-side data, but then use client-side paging, sorting etc? So right now, the table populates successfully, but the datatables functionality is not working? Commented Jul 17, 2017 at 13:46
  • @markpsmith yes, I,m doing the same as you mentioned. Can you help in finding a wayout ? Commented Jul 17, 2017 at 13:51
  • You could also use the row.add function instead of adding the rows manually to the DOM. Commented Jul 17, 2017 at 13:53

1 Answer 1

2

For this issue you need to recall DataTable after successful ajax call.

function sendSearchAccountDetailsRequest(orgQueryReqJSONString){

$.ajax({
        type : 'POST',
        url : ctx+'/SearchController',
        data: orgQueryReqJSONString,
        contentType: 'application/json',                                                                            
        success : function(response) {                   
        //process JSON response here                
        var counter = 0;
        var tableDataHTML = '';

        $.each(response.organizationDetailsList, function(counter){
            var $curr = response.organizationDetailsList[counter].organizationDetails;
            tableDataHTML += '<tr id="searched-row-'+counter+'" class="js-result-tbl-tbody-tr">'+
                             '<td>'+$curr.organizationID+'</td>'+
                             '<td>'+$curr.organizationName+'</td>'+
                             '<td>'+$curr.parentOpCoName+'</td>'+
                             '<td>'+$curr.registeredEmailID+'</td>'+
                             '<td>'+$curr.registeredPhoneNo+'</td>'+
                             '</tr>';               
            });

        $('#search-results-table-tbody').empty();
        $('#search-results-table-tbody').append(tableDataHTML);
        initResultDataTable();
        },
        error : function(response) {                 
        //handle errors here
        alert('Error !!!'+response);            
        }
});}
Sign up to request clarification or add additional context in comments.

3 Comments

.recalling initResultDataTable() is giving an alert that it can not be recalled. I had to play a trick to make it work. I removed the call from $(document).ready() and called it after successful ajax and made changes in html <tbody id="search-results-table-tbody"> <!-- remove below row and append data here --> <tr> <td colspan="5"> No data available in table. </td> </tr> </tbody>
You can apply $('#account-details-result-table').DataTable({ "order": [], "columnDefs": [ { "targets" : 'no-sort', "orderable": false, }] }); this code direct after successful ajax call instead of recalling a function.
problem will remain the same. I have reached to a solution. I used destroy() method before calling initResultDataTable() to destro DataTable instance so that i can be reinitialized. var table = $('#account-details-result-table').DataTable(); table.destroy();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.