【问题标题】:jquery data table pagination not working with json responsejquery数据表分页不适用于json响应
【发布时间】:2015-10-28 18:54:30
【问题描述】:

我尝试使用 jquery 数据表对我的 json 响应数据进行分页,但它不起作用。使用数据表 1.10.9。我的代码如下:

$(document).ready(function(){
   $('#invnReport').DataTable({
       "aoColumnDefs": [
           {'bSortable': false, 'aTargets': [] }
       ]
   });
});

$.ajax({
   data    : data,
   url     : url,
   type    : "get",
   dataType: 'json',
   error   : function(resp) {
       alert('Error');
   },
   success : function(resp) {
       render_to_inventory(resp);
   }
});

function render_to_inventory(resp){
   table = '';
   $.each(resp,function(indx,obj){
        table += '<tr>';
        table += '<td>'+parseInt(indx+1)+'</td>';
        table += '<td>'+obj.Inventory.groups+'</td>';
        table += '<td>'+obj.Inventory.quantity+'</td>';
   });

   $("tbody#invn_body").append(table);
}

这是桌子

<table class="table table-striped table table-hover table table-bordered table table-condensed" id="invnReport">
    <caption>
        Inventory Report  
    </caption>
    <thead >
        <tr style="background:#CACACA">
            <th>Sl</th>
            <th>Item</th>
            <th></th>
        </tr>
        <tr style="background:#4BB1CF">
            <th style="width:4%">No</th>
            <th>Name</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody id="invn_body">
    </tbody>
</table>

这里是json响应数据

[
    {"Inventory":{"groups":" Laptop","quantity":"1"}},
    {"Inventory":{"groups":" Laptop","quantity":"1"}},
    {"Inventory":{"groups":" Laptop","quantity":"2"}},
    {"Inventory":{"groups":" Laptop","quantity":"1"}},
    {"Inventory":{"groups":" Laptop","quantity":"-1"}},
    {"Inventory":{"groups":" Laptop","quantity":"23"}},
    {"Inventory":{"groups":" Laptop","quantity":"6"}},
    {"Inventory":{"groups":" Laptop","quantity":"13"}},
    {"Inventory":{"groups":" Laptop","quantity":"1"}},
    {"Inventory":{"groups":" Laptop","quantity":"3"}},
    {"Inventory":{"groups":" Laptop","quantity":"1"}},
    {"Inventory":{"groups":" Laptop","quantity":"1"}}
]

它使用 php 数据但不使用 json 数据。请帮助。

【问题讨论】:

    标签: jquery json datatables


    【解决方案1】:

    好的,这一次,给出的代码没有多大帮助,我不得不重写一些部分才能正常工作。这里的第一个错误是您在 DOM 就绪时调用 DataTable() 函数,而不是在 ajax 就绪时调用。在填充表格的函数中移动它应该可以解决您的问题。我为你做了一个小提琴,希望对你有帮助。

    http://jsfiddle.net/v8e24q3j/3/

    function render_to_inventory(resp) {
    table = '';
    
    
    $.each(resp, function(indx, obj) {
    
        table += '<tr>';
        table += '<td>' + parseInt(indx + 1) + '</td>';
        table += '<td>' + obj.Inventory.item_name + '</td>';
        table += '<td>' + obj.Inventory.quantity + '</td>';
    
    });
    $("tbody#invn_body").append(table);
    
    // Make the DataTable after the data is populated
    $('#invnReport').DataTable({
        "pagingType": "numbers"
    });
    
    }
    

    【讨论】:

    • 如果可行,请您批准答案吗?如果您愿意,我在这里询问更多问题
    • 感谢您的回复.. 还有一个问题。如果我想用 php 和 json 数据对同一个表进行分页怎么办。
    • 好吧,您可以使用 PHP 中的数据在页面加载时呈现 DataTable,就像使用文档就绪函数一样,并保留 ajax 函数。只需附加数据,然后再次调用 DataTable()。应该工作
    • 有趣的是,昨天我在玩初始加载,然后是 ajax 请求,然后想出了这个 JSFiddle (jsfiddle.net/annoyingmouse/6b50dtdL)。它具有初始的模拟 ajax 负载,可能只是静态数据,然后是一些后续的模拟 ajax 调用。我认为有一个唯一的 id 并且它不应该被复制是理所当然的。因此,ajax 的处理会考虑随机返回的行数,并在将它们添加到表之前检查该行是否已经存在。写起来很有趣;-)
    • 在回答您最初的问题时,请批准该答案。很高兴看到它有效
    猜你喜欢
    • 2021-11-17
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2015-03-28
    • 2011-02-04
    • 2011-03-22
    • 2019-10-11
    相关资源
    最近更新 更多