【问题标题】:search, sort and pagination of jQuery DataTables not working with ajax datajQuery DataTables 的搜索、排序和分页不适用于 ajax 数据
【发布时间】:2017-12-22 01:55:43
【问题描述】:

我能够使用 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的函数。我在 $(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);            
            }
    }); 
}

【问题讨论】:

  • 数据表有自己的AJAX option
  • @Shiffty,我知道那部分。但是,我只需要遵循上述方法。
  • 我假设您这样做是为了加载服务器端数据,然后使用客户端分页、排序等?所以现在,表格填充成功,但数据表功能不起作用?
  • @markpsmith 是的,我正在做和你提到的一样的事情。你能帮忙寻找出路吗?
  • 您也可以使用row.add 函数,而不是手动将行添加到 DOM。

标签: jquery html datatables


【解决方案1】:

这个问题需要在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);            
        }
});}

【讨论】:

  • .recalling initResultDataTable() 发出无法调用的警报。我不得不玩一个把戏让它发挥作用。我从 $(document).ready() 中删除了调用,并在成功 ajax 后调用它并在 html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 2014-03-17
  • 2023-03-20
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
相关资源
最近更新 更多