Editor: Creating a Dynamic Select Label Value Options

Editor: Creating a Dynamic Select Label Value Options

HpractitionerHpractitioner Posts: 3Questions: 1Answers: 0

I am trying to make the Group field show with dynamic data from my database. So far, I am able to do so. In my scenario, there is a row shown in Datatables Editor with an Edit icon. Once I click the Edit (pencil) icon, my Group field is successfully being filled up from the database results.

The issue is whenever I edit any row, whatever the Group mentioned for that row (let's say Carpenter ref. 1.png), my Group Select field always shows the first option i.e. Plumber (ref. 2.png). Here, I want to select the same Select field option that was mentioned earlier for the row while editing.

1.png

2.png

Following is my simplified code:

            var groupOptions = [];

            $.getJSON("getGroups.php", function(data) {
                var option = {};
                $.each(data, function(i, e) {
                    for (var iLoop = 0; iLoop < e.length; iLoop++) {
                        option.label = e[iLoop].Description;
                        option.value = e[iLoop].GroupID;
                        groupOptions.push(option);
                        option = {};
                    }
                });
            }).done(function(e) {
                editor.field('templates.group').update(groupOptions);
            });

            editor = new $.fn.dataTable.Editor({
                ajax: "getTemplates.php",
                table: "#app_template_list",
                select: true,
                fields: [{
                        label: "Group",
                        name: "templates.group",
                        type: "select"
                    }]
            });

Any idea how to achieve that?
Thanks in advance.

This question has an accepted answers - jump to answer

Answers

  • HpractitionerHpractitioner Posts: 3Questions: 1Answers: 0

    Anyone please help.
    Thanks in advance.

  • HpractitionerHpractitioner Posts: 3Questions: 1Answers: 0

    Full league of experts and not a single response.
    Surprising.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I'm not an Editor expert but having the wrong selected option has been discussed a lot on this forum. Maybe this thread or this thread will help.

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This thread is on our list to look at, you just caught us over the weekend. We'll reply shortly once we're back in the office,

    Colin

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

    Hi,

    my Group Select field always shows the first option

    That will happen if the value of the field being edited (i.e. the value of templates.group for this row) is not available in the list of option values (GroupID in this case). It is important to note that this is strongly typed as well, so it is possible that might be the cause of the issue.

    If you can give me a link to your page I can help to debug the issue.

    Allan

Sign In or Register to comment.