Refresh options values in Editor

Refresh options values in Editor

igorligorl Posts: 3Questions: 2Answers: 0

Hello,
I'm trying to refresh options used in Editor select fields, but without success. I'm able to set it on initialization time to a fixed list.

Below is my failed attempt. I need to run an ajax query to retrieve the list of options and looks like by the time I get the response, Editor is already initialized and this has no effect.

    var usersGroups = [];
    var editor = new $.fn.dataTable.Editor({
        table: '#Permissions',
        fields: [
            {
                "label": "User \/ Group:",
                "name": "user_group",
                "type": "select",
                "options": usersGroups,
                "def": function () {
                    var reqUrl = "./users";
                    $.getJSON(reqUrl, function (data) {
                        usersGroups = [];
                        $.each(data, function (i, value) {
                            usersGroups.push(value.user_name);
                        });
                    }).done(function () {
                        console.log("ok");
                    }).fail(function () {
                        console.log("error");
                    }).always(function () {
                        console.log("complete");
                    });
                }
            }

Any ideas how to solve this?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin
    Answer ✓

    You need to use field().update() to update the list of options. The problem with the above is that userGroups is only populated after Editor has already populated the fields since it is asynchronous.

    I'd suggest not using def and instead doing:

    var editor = new $.fn.dataTable.Editor({
        table: '#Permissions',
        fields: [
            {
                "label": "User \/ Group:",
                "name": "user_group",
                "type": "select"
            }
        ]
    });
    
    var usersGroups = [];
    var reqUrl = "./users";
    $.getJSON(reqUrl, function (data) {
        usersGroups = [];
        $.each(data, function (i, value) {
            usersGroups.push(value.user_name);
        });
        editor.field('user_group').update(userGroups);
    });
    

    Allan

  • igorligorl Posts: 3Questions: 2Answers: 0

    Works perfectly! Thank you.

This discussion has been closed.