3

In my django app the datatable makes a ajax REST api request where the response is as follows:

{
    "data": ["some content here"],
    "time_data": [
        {
            "Last_updated": "Jan 07 2020 06: 09 CST",
            "Next_scheduled": "Jan 07 2020 07: 09 CST"
        }
    ]
}

This is the django REST API view is as follows:

class clustersView(views.APIView):
    def get(self, request):
        results = {}
        clusters = get_collection('clusters')
        results['data'] = ClusterSerializer(clusters, many=True).data
        results['time_data'] = get_collection('time_data')
        return Response(results)

In the above response json the data key has been accessed as follows and used to populate the datatable and it works fine.

$(document).ready(function () {
    myTable = $('#table').DataTable({
        ajax: {
            "type": "GET",
            "url": "{% url 'Clusters' %}",
        },
        columns: [
            { 'data': 'Master' },
            { 'data': 'Workers' },
            { 'data': 'Build' },
            { 'data': 'Team' }]
    });
});

But the next key i.e, time_data needs to be used in a div which is outside the datatable.

How can i access the response contents in javascript? So that i can use time_data from the response.

Note: accessing the django response variable results , throws a error that the variable is not defined.

rakesh kotian
  • 232
  • 1
  • 5
  • 30
  • response.time_data will return you the array so response.time_data[0] would be the first object containing the two key/values. Assuming `response` is the object you're processing in javascript – dirkgroten Jan 07 '20 at 13:55
  • But maybe it would be more clear if you showed us your javascript code – dirkgroten Jan 07 '20 at 13:56
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Lord Elrond Jan 07 '20 at 14:00
  • @dirkgroten i have updated the question with more information, please look into it. – rakesh kotian Jan 07 '20 at 14:36
  • @ReinstateMonica whichever question link you posted does not answer my question. Also i have provided more info in the question so as it make it unique. – rakesh kotian Jan 07 '20 at 14:37
  • 1
    Look at [this](https://datatables.net/reference/event/xhr#Examples) for an example on how to access data and use it in another DOM element. And more on it [here](https://datatables.net/reference/api/ajax.json()) – dirkgroten Jan 07 '20 at 14:54

1 Answers1

2

Take a look at ajax.dataSrc. When used as callback you can manipulate the response before it is passed to DataTables, and obviously initialize or populate other elements as well :

myTable = $('#table').DataTable({
  ajax: {
    type: "GET",
    url: "{% url 'Clusters' %}",
    dataSrc: function(data) {
      doSomethingWith( data.time_data )
      return data.data
    }
  },
  columns: [ ... ]
})
davidkonrad
  • 83,997
  • 17
  • 205
  • 265