Input ipOtps Via Ajax
Input ipOtps Via Ajax
Hello,
I am not sure if this is a javascript question or datatables question, but thought I would ask and see. I am trying to input a selection for my datatables editor that is pulled from an ajax request. So, rather than:
ipOpts: [
"Selection 1",
"Selection 2"
]
I am trying to do something like this:
ipOpts: returnRoles()
Now, I have gotten this to work using $.ajax(async=false) because I can then return the text.
function returnRoles() {
var rolesArray = $.ajax({
type: "GET",
dataType: "text",
url: 'resturl.com',
async: false
}).responseText;
rolesArray = $.parseJSON(rolesArray);
return(rolesArray);
}
But, when I try and do this with async as true, the function finishes before the ajax request obviously. I have tried using .done
, using and resolving my own promises, etc, but no matter what I do, a blank response is shown on the datatables selection because it is finishing the function and taking the value before the .done
is called (im guessing). Has anyone successfully done this using an asynchronous request rather than a synchronous one, and would you be willing to share your code with me so I can check it out?
Thanks,
David
This question has an accepted answers - jump to answer
Answers
I would suggest using the
update()
method of theselect
field type:It isn't inline with the initialisation like using
returnRoles()
is - although you could pass the field name into the function and have it update that way if you wanted.Another option is to return the
options
parameter in your DataTables Ajax load. Editor will listen for that event and process theoptions
parameter. Example here - click the "Ajax load" tab below the table to see the data loaded, including theoptions
parameter.Allan
Wow, your example code worked like a charm! Thank you very much Allan!
David