Make data-order work in javascript sourced data
Make data-order work in javascript sourced data
Hi there,
There are some forum questions similar to what I'm trying to do but I haven't been able to make it work.
We are pulling data into a DataTable through javascript. I'd like to set the data-order using a different column.
However, when I try it, I'm throwing an error.
This pulls in the base data and works:
var securityTable = $('#security-table').DataTable({
"data": securitydata.guards,
"columns": [
{
"className": 'details-control',
"data": null,
"orderable": false,
//creates square for details row
"render": function () {
return '<i class="fa fa-plus-square" aria-hidden="true"></span>';
},
"defaultContent": ''
},
{ "data": "date" },
{ "data": "place" },
{ "data": "shot" }
],
"paging": false,
"searching": false
});
I've tried to modify it to this to set the order of the data column (There is one other column in which I'd like to set the data-order to as well):
var securityTable = $('#security-table').DataTable({
"data": securitydata.guards,
"columns": [
{
"className": 'details-control',
"data": null,
"orderable": false,
//creates square for details row
"render": function () {
return '<i class="fa fa-plus-square" aria-hidden="true"></span>';
},
"defaultContent": ''
},
{ "data": {
_: "date.display",
sort: "date.date_sort"
} },
{ "data": "place" },
{ "data": "shot" }
],
"paging": false,
"searching": false
});
I get this error:
DataTables warning: table id=security-table - Requested unknown parameter '[object Object]' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4
If I were doing this through html and jina I'd do something like this:
{% for d in data %}
<tr>
<td></td>
<td data-order="{{d.date_order}}">{{d.date}}</td>
<td>{{d.location}}</td>
<td data-order="{{d.shot_order}}">{{d.shot}}</td>
</tr>
{% endfor %}
The data in json form looks like this:
{
"date": "April 15, 2011",
"date_order": 1,
"reported": "Yes",
"place": "Chicago, auto parts yard",
"shot": "No one hit",
"shot_order": 24,
"blurb": "A 52-year-old guard at an auto parts lot shot at a vehicle he said was coming toward him. The man inside the vehicle, accused of stealing equipment from the lot, drove away and was not reported injured.",
"link": ""
},
Thank you!!!
Answers
Hi @Jonathon Berlin ,
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin