Populate Datatables Editor Select2 Options from Web API

Populate Datatables Editor Select2 Options from Web API

jacktrapjacktrap Posts: 14Questions: 6Answers: 0
edited November 2018 in Free community support

I have a javascript function that returns label + value array for my select2 list. The function and it's output are as below:

    function GetEtms() {
            var actionUrl =
                '@Url.RouteUrl("DefaultApi", new {httproute = "", controller = "VariableAdditionAdmin", action = "GetEtMs"})';
            $.getJSON(actionUrl,
                function(response) {
                    if (response !== null) {
                        return response;
                      
                    }
                });

        };

Output (just dummy data for now) :

[{"label":"Please Select","value":"0"},{"label":"Some Selection Label","value":"1"}]

How do I have my datatables editor field Select2 list use this javascript function to populate?

{
                        label: "ETM:",
                        name: "EtmId",
                        type: "select2",
                        "opts": {
                            "allowClear": true                    
                        }
                    },

Previously when I was using a regular dropdown I used the below (this no longer works now i'm using select2) :

  function updateEtmDropdown() {
                var actionUrl =
                    '@Url.RouteUrl("DefaultApi", new {httproute = "", controller = "VariableAdditionAdmin", action = "GetEtMs"})';
                $.getJSON(actionUrl,
                    function(response) {
                        if (response !== null) {
                            editor.field('EtmId').update(response);
                        }
                    });

            };

            updateEtmDropdown();

I have included the select2 datatables plugin in my project.

Answers

  • jacktrapjacktrap Posts: 14Questions: 6Answers: 0

    Nevermind, was all in the select2 documentation.

            {
                            label: "ETM:",
                            name: "EtmId",
                            type: "select2",
                            "opts": {
                                ajax: {
                                    url: actionUrl,
                                    dataType: "json",
                                    delay: 250,
                                    type: "GET",
                                    data: function (params) {
                                        return {
                                            q: params.term // search term
                                        };
                                    },
                                    processResults: function (data) {
                                        return {
                                            results: data
                                        };
                                    },
                                    cache: true
                                },
                                escapeMarkup: function (markup) { return markup; },
                                minimumInputLength: 1
                            }
                        }
    

    Where q is the search term. Searching done serverside.

This discussion has been closed.