سؤال

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

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top