【问题标题】:load datatable from custom response从自定义响应加载数据表
【发布时间】:2018-01-31 12:57:21
【问题描述】:

我正在处理数据表。我已经发布了 2 个与我的这个问题 thisthis 相关的问题。

AJAX 调用:

API.call("getBasicCallAnalysisData.json", 'POST', function(data) {
    basicData = data;

    createTable(basicData);
}, function(error) {
    console.log(error);
}, jsonStr);

这是来自服务器的我的 json 响应:

{"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"3217284244","cellID":"410-01-604-30226","dateTime":"2017-06-24 23:08:09.0","duration":801,"imei":"27512671195807","imsi":"35788979525680","operatorId":1,"mscId":"2","fileId":"1"},{"aNumber":"3224193861","bNumber":"3217280585","cellID":"410-01-738-13433","dateTime":"2017-06-24 06:53:13.0","duration":198,"imei":"46341570864238","imsi":"33270572168878","operatorId":2,"mscId":"4","fileId":"2"}],"draw":1,"limit":1000,"recordsFiltered":13442,"recordsTotal":13442}

HTML 表格:

<table id="table" class="display responsive nowrap" cellspacing="0" width="100%">
                                <thead style="background-color:#303641;">
                                    <tr>
                                        <th>ANUMBER</th>
                                        <th>BNUMBER</th>
                                        <th>DATETIME</th>
                                        <th>DURATION</th>
                                        <th>IMEI</th>
                                        <th>IMSI</th>
                                        <th>CELLID</th>
                                        <th>OPRID</th>
                                        <th>MSC ID</th>
                                        <th>FILE ID</th>
                                    </tr>
                                </thead>
                            </table>

这是我加载数据表的功能,但我面临两个错误:

function createTable(data) {
    console.log(data);
    $('#table').DataTable({
        "processing": true,
        "serverSide": true,
        "bFilter": false,
        "iDisplayLength": configData,
        dom: 'Bfrtip',
        buttons: ['colvis', 'print', 'csv', 'excel', 'pdf'],
        searching: false,
        language: {
            buttons: {
                colvis: 'Show/Hide Columns'
            }
        },
        "aaData": data


    });
}

参数data是来自服务器的响应。错误是:

DataTables 警告:table id=table - 请求的未知参数 'aNumber' 表示第 0 行第 0 列。有关此错误的更多信息, 请看http://datatables.net/tn/4

无效的 JSON 响应。

任何想法,我如何使用 JSON 响应填充数据表?

【问题讨论】:

  • 您的 json 似乎有效,根据json Lint 中的测试...您可以检查变量data 的类型...也许它是一个字符串。试试:console.log( typeof(data) );。它应该返回object。对于另一个错误...我认为您的 HTML 需要进行调查。
  • @LouysPatriceBessette 是的,console.log( typeof(data) );返回object。接下来是什么 ?如何解决这个问题?
  • 在“HTML”选项卡中是否有表头 &lt;thead&gt; 以及包含列标题 like here 的行?
  • 您会发布您的 HTML 以及您使用的数据表版本吗?
  • 是的,我已经在我的问题中发布了我的 HTML 表格

标签: javascript jquery json datatables


【解决方案1】:

有两种可能将您的 json 放入 DataTables...

  1. 使用jQuery Ajax request
  2. 使用DataTables remote preocessing

你使用第一个。

您的代码有两个主要问题:

  1. 一个多余的 DataTables 属性。
  2. 一个有效的 JSON,但结构不符合 DataTables 的预期。

#1
属性"serverSide": true, 基本上表示您打算使用“远程处理”。因此,DataTables 现在需要一个包含 PHP 地址的“ajax”属性来获取 JSON。此缺失属性触发了“无效 JSON”错误。

我现在听到你大声喊着不清楚的错误信息!!! (笑)

#2
DataTables 期望将数据结构化为整个表的数组...
该数组必须保存“行”值的数组。示例here

这不是你实际拥有的。
所以我做了一个函数来“重新排列”数据以满足 DataTables 的期望。

console.clear();

// A function to run trhough your json and restructure the data as an array of array.
function arrangeData(data){
  var datatableData = [];

  for(i=0;i<data.length;i++){
    var row = [
      data[i].aNumber,
      data[i].bNumber,
      data[i].cellID,
      data[i].dateTime,
      data[i].duration,
      data[i].imei,
      data[i].imsi,
      data[i].operatorId,
      data[i].mscId,
      data[i].fileId
    ];
    datatableData.push(row);
  }
  console.log(datatableData);

  return datatableData;
}

// Your original function not really changed...
function createTable(data) {

  $('#table').DataTable({
    "processing": true,
    //"serverSide": true,     // This works with the "remote preocessing" ajax attribute.
    "bFilter": false,
    //"iDisplayLength": configData,   // I commented this one only because I did not have the "configData" variable.
    dom: 'Bfrtip',
    buttons: ['colvis', 'print', 'csv', 'excel', 'pdf'],
    searching: false,
    language: {
      buttons: {
       colvis: 'Show/Hide Columns'
      }
    },
    "aaData": arrangeData(data)   // I'm calling the "arrange" function here.
  });
}

// This is to simulate the response you get form jQuery Ajax.
var dataset = {
  msg:null,
  code:null,
  status:null,
  result:[
    {
      "aNumber":"3224193861",
      "bNumber":"3217284244",
      "cellID":"410-01-604-30226",
      "dateTime":"2017-06-24 23:08:09.0",
      "duration":801,
      "imei":"27512671195807",
      "imsi":"35788979525680",
      "operatorId":1,
      "mscId":"2",
      "fileId":"1"
    },
    {
      "aNumber":"3224193861",
      "bNumber":"3217280585",
      "cellID":"410-01-738-13433",
      "dateTime":"2017-06-24 06:53:13.0",
      "duration":198,
      "imei":"46341570864238",
      "imsi":"33270572168878",
      "operatorId":2,
      "mscId":"4",
      "fileId":"2"
    }
  ],
  "draw":1,
  "limit":1000,
  "recordsFiltered":13442,
  "recordsTotal":13442
};

// Call the table draw function... Certainly in your ajax success callback.
createTable(dataset.result);

CodePen

【讨论】:

  • 所以,你是说我不能以我正在做的方式实现服务器端处理(分页)?
  • 我没这么说!我说有两种获取数据的方法......你使用第二种方法的属性来做到这一点,这导致了关于json的错误。 --- 那么,数据的结构就很重要了。
  • 您提供的解决方案不适用于服务器端处理。
  • 不...因为 JSON 会在 Datatables 中正确显示。但是,如果您可以更改服务器生成 json 的方式...我没有阅读太多有关“远程处理”的内容...也许您实际拥有的数据结构已接近要求...比现在更接近。我不知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多