Can I load text / flat json from datatable.net?

十年热恋 提交于 2019-12-13 03:43:37

问题


Hi i have been using datatables but have not tried loading data view pure json object.

I always use sAjax source so it always point to a url.

My question is how to pass a json object directyl so that it will eliminate the url


回答1:


Use Json.parse and add the values in a loop:

$(function() {
  json = '{ "employees" : [' +
    '{ "firstName":"John" , "lastName":"Doe" },' +
    '{ "firstName":"Anna" , "lastName":"Smith" },' +
    '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

  parsedJson= JSON.parse(json);
  var otable = $("#datatable").dataTable();

  //Forgot to clear the table first
  otable.fnClearTable();

  $.each(parsedJson.employees, function(key, value) {
    otable.dataTable().fnAddData([
      value.firstName,
      value.lastName,
    ]);
  })

});

Here's a Plunker



来源:https://stackoverflow.com/questions/21924803/can-i-load-text-flat-json-from-datatable-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!