Dynamically set URL of datatables editor create / update / delete
Dynamically set URL of datatables editor create / update / delete
cougarkevin
Posts: 7Questions: 4Answers: 0
I am trying to dynamically set the ajax url's for the datatables editor via a call to a function. The following works:
create: {
"url": "http://10.20.40.60/api/",
But this does not work:
create: {
"url": getUrl(),
...
function getUrl() {
return "http://10.20.40.60/api/"
}
Any thoughts on how I do this?
Best Wishes,
ks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Posted in error - sorry.
The problem with using
"url": getUrl(),
is thatgetUrl()
is executed at the time when the Editor is initialised. I'm guessing that isn't what you want.Editor doesn't really have a way to dynamically change the target URLs assigned to it, and I don't believe jQuery allows
url
as a function (although that would be useful in this case!) - so what you would need to do is useajax
as a function and provide your own$.ajax
call to the server. That will give you the option of executinggetUrl()
dynamically.If it doesn't need to be dynamic - i.e. it always returns the same static string, the above should actually work - although you would want to define
getUrl()
before you use it.Allan
Allan,
Thanks for you help. We just purchased a license to datatables and I'm expected to get this up and running asap!
For the next person who wants to do this with a REST service here is the code that worked for me. For the delete, we pass a string of ID's to the REST service.
getUrl() returns the endpoint of the service.
POST to add a record
PUT to edit a record
DELETE to delete one or more records