Input ipOtps Via Ajax

Input ipOtps Via Ajax

CoreBioCoreBio Posts: 12Questions: 5Answers: 0

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

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    I would suggest using the update() method of the select field type:

        var rolesArray = $.ajax({
            dataType: "json",
            url: 'resturl.com',
            success: function ( json ) {
              editor.field( 'myField' ).update( json );
            }
        });
    

    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 the options parameter. Example here - click the "Ajax load" tab below the table to see the data loaded, including the options parameter.

    Allan

  • CoreBioCoreBio Posts: 12Questions: 5Answers: 0

    Wow, your example code worked like a charm! Thank you very much Allan!

    David

This discussion has been closed.