George
George

Reputation: 746

Codeigniter + Select2 set default value for use in edit forms

To populate my dropdown list I’m using ajax to call a function and get the records I want then I use $.map to change the id and text to the column names I use.

All work perfect but now I’m stuck on how can I give a default value to the dropdown :

$(".js-firme-data-array").select2(  {
        ajax: {
            // The number of milliseconds to wait for the user to stop typing before issuing the ajax request
            delay: 400,
            url: "<?php echo site_url('proiecte/get_firme') ?>",
            dataType: "json",
            cache: "true",

            data: function (params) {
                return {
                q: params.term, // search term
                page: params.page,

                };
            },

            processResults: function (data) {
                return {
                    results: $.map(data, function(obj) {
                        return { id: obj.id, text: obj.denumire };
                    })
                };
            },
        },
    });

Upvotes: 1

Views: 1890

Answers (1)

George
George

Reputation: 746

Found the solution if someone else is looking for this :

initSelection : function (element, callback) {
    var data = {id: "your id", text: "your text"};
    callback(data);
},

Upvotes: 2

Related Questions