将数据表与自动完成中的JSON数组绑定

时间:2013-11-21 20:39:32

标签: javascript jquery json autocomplete datatable

我正在使用自动填充字段从服务器获取数据并在数据表中显示:

$( “#姓名”)。自动完成({

    source: function (request, response) {
         $.ajax({
             url: "http://myhost.com/webservices/test3.cfm",
             data: request,
             success: function (data) {
                    $('#results').dataTable({
                        "bProcessing": true,
                        "bJQueryUI": true,
                        "bLengthChange": true,
                        "bFilter": true,
                        "bAutoWidth": false,
                        "bRetrieve" : true,
                        "aaData": data , 
                        "aoColumns": [
                            { "sTitle": "Name", "sName": "name" },
                            { "sTitle": "Title", "sName": "title" },
                            { "sTitle": "Organization", "sName": "organization" },
                            { "sTitle": "Email", "sName": "email" },
                            { "sTitle": "Status", "sName": "status" }
                        ]
                    });
             }
         });
    }

从ajax调用返回的数据是:
[[“Steven,Grek”,“President”,“Sands Corp.”,“steven @ yahoo.com”,“1”],[“Steven,Grek”,“Associate”,“Alliance Ltd.”,“steven” @ yahoo.com“,”1“],[”Steven,Grek“,”总统“,”林产品协会“,”steven @ yahoo.com“,”1“]]

我收到以下错误:

  • DataTables warning(table id ='results'):从第0行的数据源请求未知参数'1'
  • DataTables警告(表id ='结果'):从第9行的数据源请求未知参数'1'
  • 显示2,147条记录中的1到10条

如果我更换“aaData”:数据
根据数据的反应:
“aaData”:[[“Steven,Grek”,“President”,“Sands Corp.”,“steven @ yahoo.com”,“1”],[“Steven,Grek”,“Associate”,“Alliance Ltd. “,”steven @ yahoo.com“,”1“,[”Steven,Grek“,”总统“,”林产品协会“,”steven @ yahoo.com“,”1“]

它有效。

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

在同事的帮助下计算出来: 来自我的ajax调用的数据类型是一个字符串。

$("#firstname").autocomplete({
    source: function (request, response) {
     $.ajax({
         url: "http://myhost.com/webservices/test3.cfm",
         data: request,
         success: function (data) {
            var obj = jQuery.parseJSON(data);      <---- typeof data is a string
            $('#results').dataTable({
                "bProcessing": true,
                "bJQueryUI": true,
                "bLengthChange": true,
                "bFilter": true,
                "bAutoWidth": false,
                "bRetrieve" : true,
                "aaData": obj,                 <---- Use the parsed json object instead
                "aoColumns": [
                    { "sTitle": "Name", "sName": "name" },
                    { "sTitle": "Title", "sName": "title" },
                    { "sTitle": "Organization", "sName": "organization" },
                    { "sTitle": "Email", "sName": "email" },
                    { "sTitle": "Status", "sName": "status" }
                ]
            });
         }
     });
   },

});