select2-multi-checkboxes
select2-multi-checkboxes copied to clipboard
set selected options after page refresh
Any idea how to set already selected options, if my form at submit has errors, and the framework redirects back to the form with error messages. I tried adding this as parameter but nothing selected:
data: [
{ id: 571, text: 'Business and Finance Summit 2017'}
]
$(".selectMulti").val([571, 572]); ?
This is a realy not intuitive thing. Usually you need 2 fields for the process. 1st field with saved values comma separated i.e. in a database on the server, 2nd is the multi select field itself. For automation I'm using a naming convention for the multi select field: id of field i.e. = name -> id of multi select field = name + 'Ms'.
For init of all selected values and all multi select fields on a page I use this function...
(function initMultiSelects() {
// select all multi select objects
var data = $('.msCheckboxDeliver');
$.each(data, function(key, msObj) {
// set datas only if object has an id and datas are there
if (msObj.id != '' && msObj.value) {
// get the comma separated values as array
var dataArr = msObj.value.split(',');
// set select2 datas
$('#' + msObj.id + 'Ms').data('select2').data(dataArr);
// check options of select2MultiCheckboxes
$.each(dataArr, function(key, value) {
// set values
var obj = $('#' + msObj.id + 'Ms option[value=' + value + ']');
// set option class checked
obj.addClass('checked');
});
}
});
})();