jquery - How to set value in Select2 when using remote source in 4.0.3 js -


there scenario in application have show tags saparated value in control choose select 2 .

  <select id="ddlgulfemployee" multiple="multiple" style="display: none;   width:   100%;" class="form-control"></select>   </script>`$("#ddlgulfemployee").select2({     ajax: {         url: '@system.web.virtualpathutility.toabsolute("~/home/getmasteruser")',// '@url.action("getmasteruser","home") %>', //"../getmasteruser",         datatype: 'json',         delay: 250,         data: function (params) {             debugger;             return {                 q: params.term, // search term                 page: params.page             };         },         processresults: function (data) {             debugger;             var arr = []             $.each(data, function (index, value) {                 //debugger;                 arr.push({                     id: value.id,                     text: value.firstname                 })             })             return {                 results: arr             };         },         cache: true     },     escapemarkup: function (markup) { return markup; }, // let our custom formatter work     minimuminputlength: 1,     templateresult: function (people) {         debugger;         //debugger;         if (people.loading)             return people.text;          var markup = '<option value="' + people.id + '">' + people.text + '</option>';          return markup;     },     templateselection: function (people) {         debugger;         return people.value || people.text     }     //,     //initselection: function (element, callback) {     //    debugger;     //    callback($.map(element.val().split(','), function (id) {     //        return { id: id, text: id };     //    }));     //} }); $("document").ready(function () {//i want option      //1 russell      $('#ddlgulfemployee').select2('val', ["test1", "test2"], true);  }); 

`

when saving data , getting remote source working fine problem when want show saved value control on page load during editing .

please me.

thanks , regards

please try code.

here need pass data in format of key , values.

$("document").ready(function () {         $("#ddlgulfemployee").select2({                data: [                 {                   id: '1',                   text: 'option 1'                 },                 {                   id: '2',                   text: 'option 2'                 },                 {                   id: '3',                   text: 'option 3'                 }                ]         });     }); 

you can check in select2 here

hope helps.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -