搜索,排序和分页不使用ajax数据的jQuery DataTables

时间:2017-07-17 13:35:30

标签: jquery html datatables

我可以使用Ajax在我的jQuery DataTable中填充数据。但在此之后,jQuery DataTables的搜索,排序和分页已停止工作。请帮忙。

这是我的HTML代码:

<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>

下面是初始化搜索结果的jQuery DataTable的函数。我&#39;在$(document).ready()中调用它:

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

这是我的ajax电话:

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);            
            }
    }); 
}

1 个答案:

答案 0 :(得分:2)

对于此问题,您需要在成功调用ajax后调用DataTable。

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);            
        }
});}