WebService and ajax.dataSrc Object Name
WebService and ajax.dataSrc Object Name
Hello,
I'm having some slight trouble with the ajax.dataSrc function. It appears that it will only accept "data" and "aaData" for the array/object name. Is there a way to override this?
I'm currently utilizing a web service to return the desired JSON results back for the table. However, the issue I am running into is that the JSON returned uses the a different name. Is it possible to override the "data" and/or "aaData"?
Below is an example of what our JSON returned result.
{
"employees": [
{
"FirstName": "Tiger",
"LastName": "Nixon",
"Position": "System Architect",
"Office": "Edinburgh",
"Salary": "$320,800"
},
{
"FirstName": "Garrett",
"LastName": "Winters",
"Position": "Accountant",
"Office": "Tokyo",
"Salary": "$170,750"
},
{
"FirstName": "Donna",
"LastName": "Snider",
"Position": "Customer Support",
"Office": "New York",
"Salary": "$112,000"
}
]
}
The code I'm using to trigger this is inside a jQuery button click function.
$("#btnCplSearch").click(function () {
var method = "GetDocuments";
var keyword = $("#txtSearchTerm").val();
if (keyword == "")
{
ErrorMsg("No Keyword found.")
//GetData("GetDocuments", "keyWords=test", true, true)
}
else {
var wsUrl = "http://localhost/ClinicalDocsSearch/DocumentSearch.svc/" + method + "?" + "keyWords=" + keyword;
dt.clear();
dt.ajax.url(wsUrl);
dt.draw();
dt.ajax.reload();
}
});
Thanks in advance!
This question has an accepted answers - jump to answer
Answers
ajax.dataSrc provides the option to define your own string.
https://datatables.net/reference/option/ajax.dataSrc
Hello!
I've been looking at that page and can't seem to wrap my head around it. If I utilize it exactly as it is on the page on initialization, it works. I can define it. However, I'm trying to define it on the fly after the dataTable has been initialized. using
$('#cplSearchResult').DataTable.ajax.dataSrc("GetDocumentsResult");
doesn't give me any errors, but any javascript I put after this line does not fire. And Thanks for the quick reply!!!
is GetDocumentsResult a function? then try
$('#cplSearchResult').DataTable.ajax.dataSrc(function(result) {GetDocumentsResult (result);})
GetDocumentsResult(result){
return result.employees;
}
Are you using the
ajax
option to configure the DataTable with the Ajax URL?If so, use:
That just tells DataTables the data array is in the
employees
parameter.Allan
@Bindrid - Thanks! I'll modify the above and give it a try. The first attempt didn't work but maybe I missed something on my end.
@allan - I'm trying to use the ajax option but the table needs to be empty when the page initially loads. The table only loads when someone does a search. When I leave the url blank on the load provides an error. I'm dynamically trying to load the URL that connects to a web service to pull the results.
If I'm not mistaken, the code piece in your comment only works on the initialization of the datatable.
Here is something I did to keep things simple.
I tend to put my table declarations in a function to make it easier to reuse.
In this case in particular I have to be able to switch between single and multi selects.
This is a search page to find system users (the search text is applied against there user name, email, formal name, etc).
My Web Method:
Note: I pulled off the keydown/keypress put on by Datatable and replaced it with my own. User has to click a button or hit the button to fire off the ajax reload. So for my button click event handler I have:
And my table declaration. In particular note how I did the ajax section.