使用ajax将数据阵列加载到数据表中

时间:2014-12-09 16:26:35

标签: ajax datatables

我正在尝试将以下数组加载到数据表中,

  var array=  {
        "rowNumber": 2,
        "mailingID": 3,
        "firstname": "Bruce",
        "lastname": "Ballister",
        "organization": "Apalachee Regional Planning Council",
        "address1": "20776 Central Avenue East",
        "address2": "Suite 1",
        "city": "Blountstown",
        "state": "FL",
        "zip": 32424
      }

但是,表没有将数组加载到datatable中。此外,我可以看到该阵列已被接收到浏览器"响应"标签。我想我错过了一些东西。

我的代码:

$.ajax({
            type : "POST",
            url : "getLabels.jsp",
            data : "mailingID=" + selectedValue, // posCodeSelected
            success : function(data) {
             var array= data;
             $(document).ready(function() {
                    $('#printIDs').dataTable( {
                        "ajax": array, /* i think change is needed here*/
                        "columns": [
                            { "data": "rowNumber" },
                            { "data": "mailingID" },
                            { "data": "firstname" },
                            { "data": "lastname" },
                            { "data": "organization" },
                            { "data": "address1" },
                            { "data": "address2" },
                            { "data": "address3" },
                            { "data": "address4" },
                            { "data": "city" },
                            { "data": "state" },
                            { "data": "zip" }

                        ]
                    } );
                } );

            },
            error : function(response) {
                var responseTextObject = jQuery
                        .parseJSON(response.responseText);
            }
        });

小提琴链接:http://jsfiddle.net/kpqru5hm/

1 个答案:

答案 0 :(得分:1)

<强>已更新

您使用的数组结构似乎对数据表不正确。

您的代码应该是:

<强> HTML

<table id="printIDs" class="table">

</table>

<强> JavaScript的:

var dataSet = [
    ['Misc','IE Mobile','Windows Mobile 6','-','C'],
    ['Misc','PSP browser','PSP','-','C'],
    ['Other browsers','All others','-','-','U']
];

$(document).ready(function() {
    $('#example').dataTable( {
        "data": dataSet,
        "columns": [
            { "title": "Engine" },
            { "title": "Browser" },
            { "title": "Platform" },
            { "title": "Version", "class": "center" },
            { "title": "Grade", "class": "center" }
        ]
    } );   
} );

这里是小提琴:http://jsfiddle.net/harshulpandav/kpqru5hm/2/

了解更多信息:http://www.datatables.net/examples/data_sources/js_array.html