I've tried initSelection
in select2 to load some pre selected data in my multiple select box. User can select multiple options while creating an item and when s/he tries to edit that item again s/he can see all the selected values chosen by him/her and remove/add values from select.
It works fine and loads all pre selected options. It also shows them in multiple select box as selected. But when I submit my form select input has null value.
Here's my script
$('.select2').select2({
minimumInputLength: 2,
"language": {
"noResults": function(){
return "{{Lang::get('lang.no-department-found')}}";
},
searching: function() {
return "{{Lang::get('lang.searching')}}";
},
inputTooShort: function(args) {
// args.minimum is the minimum required length
// args.input is the user-typed text
var remaining = args.minimum - args.input.length;
return "{{Lang::get('lang.please-enter')}} "+remaining+" {{Lang::get('lang.or-more-character')}}";
},
},
ajax: {
url: "{{URL::route('api-get-department-names')}}",
dataType: 'json',
delay: 250,
type: "GET",
quietMillis: 50,
data: function (params) {
return {
name: params.term, // search term
page: params.page
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
initSelection: function(element, callback) {
var id;
id = $(element).val();
if (id !== "") {
$.ajax({
url: "{{URL::route('get-canned-department', $canned->id)}}",
type: "GET",
dataType: "json",
}).done(function(data) {
callback(data);
});;
}
}
});
HTML
<select class="form-control select2" name="d_id[]" multiple="multiple" data-placeholder="{!! Lang::get('lang.enter-department-name') !!}" style="width: 50%;" disabled="true">
</select>
Follow the link to see select box options and it's value as null. https://drive.google.com/file/d/0B_JQE_eICBj6elpRYVZhU2w5eTA/view?usp=sharing
Also when I try to remove one pre loaded option it removes all pre loaded options from the select box.
Please help me to set values for pre populated options in select2 and handle remove values option. TIA