Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a local JSON object as a data source for jQuery DataTables

Tags:

I have a local JSON object formatted like this:

[{     "id": "58",     "country_code": "UK",     "title": "Legal Director",     "pubdate": "2012-03-08 00:00:00",     "url": "http://..." },{     "id": "59",     "country_code": "UK",     "title": "Solutions Architect,",     "pubdate": "2012-02-23 00:00:00",     "url": "http://..." },{     // ....more of the same...... }] 

I would like to set this as the data source for a jQuery datatable and have tried this:

testdata = '{{ jobsJSON | raw }}'; //twig template tag console.log(testdata); $('#test').dataTable({     "aoData": testdata,     "aoColumns": [         { "mDataProp": "id" },         { "mDataProp": "country_code" },         { "mDataProp": "title" },         { "mDataProp": "pubdate" },         { "mDataProp": "url" }     ] }); 

The DataTables plugin loads and attempts to draw the table but gives the error 'No data available in table'

I am not making an AJAX call and just want to access the JSON object from a local JS variable.

like image 356
codecowboy Avatar asked Mar 12 '12 15:03

codecowboy


People also ask

How do you populate a DataTable?

Begin on the indicator's definition page. For data source, select populate from a data table and choose the table that contains information for this indicator. Next, select the calculation. You can either count all rows, count unique sets of columns, or get a sum or average of a column of numeric data.

Is jQuery DataTable open source?

DataTables is free, open source software that you can download and use for whatever purpose you wish, on any and as many sites you want.

What is dataSrc DataTable?

dataSrc( data ) Description: As a function dataSrc provides the ability to manipulate the data returned from the server from one form into another. For example, if your data is split over multiple arrays you might combine it into a single array to return for processing and display by DataTables.

What is Deferrender in DataTables?

This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.


1 Answers

The property to supply your own data is aaData NOT aoData:

testdata = [{"id":"58",...}]; // local object  $('#test').dataTable({     "aaData": testdata,     "aoColumns": [         { "mDataProp": "id" },         { "mDataProp": "country_code" },         { "mDataProp": "title" },         { "mDataProp": "pubdate" },         { "mDataProp": "url" }     ] }); 

Working fiddle

like image 50
Rory McCrossan Avatar answered Oct 05 '22 01:10

Rory McCrossan