Json format for dataTable using ajax data
Json format for dataTable using ajax data
We have a data source which does not provide standard json format that datatable uses. Following is an example:
{
"@timestamp": "20220722T153302,57Z",
"@toplevelentries": "6049",
"viewentry": [{
"@position": "1",
"@unid": "F743A6910453353D8625880E005340A1",
"@noteid": "1AB6",
"@siblings": "6049",
"entrydata": [{
"@columnnumber": "0",
"@name": "MCAN8",
"number": {
"0": "100"
}
},
{
"@columnnumber": "1",
"@name": "MCDL01",
"text": {
"0": "Gary test 1st Lease Accountant Notification"
}
},
{
"@columnnumber": "2",
"@name": "Manager",
"text": {
"0": "Huang, Gary"
}
},
{
"@columnnumber": "3",
"@name": "$13",
"text": {
"0": "CR"
}
},
{
"@columnnumber": "4",
"@name": "MCTOU",
"number": {
"0": "478.41"
}
},
{
"@columnnumber": "5",
"@name": "MCSTYL",
"text": {
"0": "OPEN"
}
}
]
},
{
"@position": "2",
"@unid": "A606B6BCF46029028625880E005340AD",
"@noteid": "1AE6",
"@siblings": "6049",
"entrydata": [{
"@columnnumber": "0",
"@name": "MCAN8",
"number": {
"0": "107"
}
},
{
"@columnnumber": "1",
"@name": "MCDL01",
"text": {
"0": "LUCY CHRISTENSEN TRUST"
}
},
{
"@columnnumber": "2",
"@name": "Manager",
"text": {
"0": "Culver, Brent"
}
},
{
"@columnnumber": "3",
"@name": "$13",
"text": {
"0": "CR"
}
},
{
"@columnnumber": "4",
"@name": "MCTOU",
"number": {
"0": "478.41"
}
},
{
"@columnnumber": "5",
"@name": "MCSTYL",
"text": {
"0": "OPEN"
}
}
]
}
]
}
This obvious won't work for following datatable definition:
$(document).ready(function () {
var table = $('#example').DataTable({
ajax: 'ajxemp?open',
columns: [
{
className: 'dt-control',
orderable: false,
data: null,
defaultContent: '',
},
{ data: 'name' },
{ data: 'position' },
{ data: 'office' },
{ data: 'salary' },
],
order: [[1, 'asc']],
});
I replaced { data: 'name' } ... part with following:
{ viewentry.entrydata[0].number['0'] },
{ viewentry.entrydata[1].text['0'] },
{ viewentry.entrydata[2].text['0'] },
{ viewentry.entrydata[3].text['0']},
{ viewentry.entrydata[4].number['0'] },
{ viewentry.entrydata[5].text['0'] },
but doesn't have any luck. How do you define the column data in order to use our data source? We can't change our format because of the application limitation.
Thanks for your help.
Replies
You need to tell DataTables to find your data in the
viewentry
array usingajax.dataSrc
.So you would use:
Then the
columns.data
options would be based on that. Egentrydata.0.text.0
.Allan
Thanks!. I re-did my datatable definition as follows:
I am getting Uncaught SyntaxError: Unexpected identifier at columns: [
My data or viewentry 'farmnumber' should come from '100' in below:
and 'farmname' should come from 'Gary test 1st Lease Accountant Notification' in below:
etc.,, etc.
I just don't know how to reference viewentry column data.
Missing a comma after the
ajax
object closing bracket - line 7 in the aboveAlso change:
To be:
And the same for the other columns.
Allan