How to show JSON without 'data' key in DataTable?
How to show JSON without 'data' key in DataTable?
dunkubok
Posts: 1Questions: 1Answers: 0
I have the following Ajax request JSON-format:
Array:
Object:
Object (key = 'fields'):
Values to be shown
Object:
Object (key = 'fields'):
Values to be shown
Object:
Object (key = 'fields'):
Values to be shown
How can I add this data format to DataTable? I think I should somehow get rid of the key values in 'columns'.
Any idea?
$('#grid').DataTable({
"ajax": demand,
"columns": [
{"fields": model"},
{"fields": "product"},
{"fields": "type"},
{"fields": "build"},
{"fields": "sdnad"},
{"fields": "rcnad"},
{"fields": "sdeud"},
{"fields": "rceud"},
{"fields": "smsd"},
{"fields": "smrc"},
{"fields": "notes"},
],
initComplete: function () {
this.api().columns().every(function () {
var column = this
var select = $('<select><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
)
column
.search(val ? '^' + val + '$' : '', true, false)
.draw()
})
column.data().unique().sort().each(function (d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
})
})
}
})
This discussion has been closed.
Answers
I think DataTables relies on the JSON being formatted that specific way, but more experienced users may chime in with some other options.
Here's what I do:
1) Include this at the top of the target page for the AJAX request:
2) Construct sql statement as
$sql
and run:
$records = $db->sql( $sql )->fetchAll();
to get the results.3) Modify
$records
if necessary4) Return to the requesting page in the proper format:
This page discusses the data structures supported by Datatables:
https://datatables.net/manual/data/
Datatables doesn't have a
fields
object for columns. you would usecolumns.data
. For example:I'm not clear what your JSON format is but, if using objects, it will need to look similar to this:
Here is an ajax with objects example:
https://datatables.net/examples/ajax/objects.html
Take a look at the "Ajax" tab.
Or if you want to use arrays you can use this example:
https://datatables.net/examples/ajax/simple.html
Kevin
Can you show us an actual example of the data please? I'm sore certain, but from your description you might be able to use
columns.data
's ability to use nested data.Allan