jQuery DataTables 列出 JSON 数据

标签 jquery jquery-ui datatable jquery-plugins datatables

$.ajax({
        url: "https://localhost:450/rest/products?pageNumber=1&pageCount=100",
        type:"post",
        dataType:"json",
        traditional:true,
        contentType : "application/json; charset=utf-8",
        processData : false,
        data:
            JSON.stringify(hostAddresses)

        ,



       success:function (response) {
            console.log(response);
            var trHTML ='';
           $('.js-exportable').DataTable({
               dom: 'Bfrtip',
               responsive: true,
               buttons: [
                   'copy', 'csv', 'excel', 'pdf', 'print'
               ]
           });
            for(var i = 0 ; i < response.length ; i++)
            {

                for(var j = 0 ; j < response[i].Products.length ; j ++)
                {


                   // trHTML  += '<tr><td>' +response[i].IP + '</td><td>' + response[i].Products[j].Product + '</td><td>' + response[i].Products[j].CVECount + '</td>';
                    //console.log(response[i].Products[j].Product);
                    //console.log(trHTML);
                    //$('#ProductsTableBody').append(trHTML);
                }

            }


        },
        error:function (xhr) {
            console.log("Error...");

        }
    })

我有这个代码。注释行中的代码是添加到正则排序列表的过程。但不适用于 jQuery 数据表。我怎样才能做到这一点。

其次,添加这些数据表后,如何将 id 分配给列表项,以便在单击时引导与该列表项关联的特殊页面。

最佳答案

这里的问题是您在填充表之前初始化 DataTables。因此它只能处理空表。

您需要将 DataTable 初始值设定项移至 for 循环之后:

    success:function (response) {
        console.log(response);
        var trHTML ='';

        for(var i = 0 ; i < response.length ; i++)
        {

            for(var j = 0 ; j < response[i].Products.length ; j ++)
            {


                trHTML  += '<tr><td>' +response[i].IP + '</td><td>' + response[i].Products[j].Product + '</td><td>' + response[i].Products[j].CVECount + '</td>';
                console.log(response[i].Products[j].Product);
                console.log(trHTML);
                $('#ProductsTableBody').append(trHTML);
            }

        }
        $('.js-exportable').DataTable({
           dom: 'Bfrtip',
           responsive: true,
           buttons: [
               'copy', 'csv', 'excel', 'pdf', 'print'
           ]
       });

    },

关于jQuery DataTables 列出 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337427/

相关文章:

jquery - 将 <div> 附加到 jQuery slider 句柄

c# - 在 C# 中迭代​​数据行

javascript - 嵌套表单密封在 Jquery 中不起作用

javascript - 在文本框 html 中插入值选择

jquery - 错误: cannot call methods on autocomplete prior to initialization; attempted to call method 'destroy'

javascript - 根据显示的页面部分更新 JQuery UI slider

javascript - 如何将 jQuery 的 insertBefore() 函数与 HTML 的 javascript 字符串一起使用?

javascript - 如果表格空白并显示文本,则隐藏按钮

c# - 删除数据表中的重复项

r - 根据其他列的条件提取数据帧行的子集