问题
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