Reload Datatable Viewer with new AJAX Source .load()

Reload Datatable Viewer with new AJAX Source .load()

bvelasquezbvelasquez Posts: 28Questions: 7Answers: 0

I am using the client side version of datatabes viewer and editor to work with AJAX sourced JSON data.
It seems like a simple thing, but I can't seem to get this to work.

I need to clear the table and load new data on demand using another query. Same, structure same endpoint, just different query string operations.

I have tried to use ajax.url().load() , which seems like the right thing do use, but my server produces data in the following format:

[{
"Comments": "",
"Hours": 5.5,
"Lunchbreak": 5.5,
"ClockOut": "2017-04-19T17:00:00",
"ClockIn": "2017-04-19T06:00:00",
"WorkDateKey": "2017-04-20T00:00:00",
"UserID": "BVelasquez",
"SeqNumForFrontEnd": 1066
}]

Not the format datatables would like:

{
"data": [
{
"DT_RowId": "row_29",
"first_name": "Fiona",
"last_name": "Green",
"position": "Chief Operating Officer (COO)",
"office": "San Francisco",
"extn": "2947",
"salary": "850000",
"start_date": "2010-03-11"
}
]
}

I'm my initialization, I use the "dataSrc": "" and everything works like a dream. But when I try to load the new data using ajax.url().load(). It doesn't like it :neutral:

How do I use ajax.url().load() and still be able to tell the viewer to expect the format in a plain array and not an object?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 22,041Questions: 26Answers: 5,083
    Answer ✓

    I believe the same dataSrc is used when using ajax.url().load(). Looks like you are returning an array of objects and it sounds like the same structure is returned when using ajax.url().load(). But its not working for you. What errors do you get?

    Maybe you can post your Datatables code and the JSON results from the initial load and the ajax.url().load().

    Kevin

  • allanallan Posts: 64,599Questions: 1Answers: 10,683 Site admin
    Answer ✓

    Yup, Kevin is spot on as usual. ajax.dataSrc will be reused when using ajax.url().load(), so you need to make sure it is returning the same structure as when the table was initially loaded.

    Allan

  • bvelasquezbvelasquez Posts: 28Questions: 7Answers: 0

    Thank you so much guys, that is what I thought ~ it turns out my query was just wrong. :smile:

    I was using a "YYYY/MM/DD" format when I should have been using a "YYYY-MM-DD" format in my query string. Silly me, thanks Kevin and Allan for the quick reply.

This discussion has been closed.