select2-multi-checkboxes icon indicating copy to clipboard operation
select2-multi-checkboxes copied to clipboard

set selected options after page refresh

Open subdesign opened this issue 7 years ago • 2 comments

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'}
]

subdesign avatar Oct 27 '17 09:10 subdesign

$(".selectMulti").val([571, 572]); ?

benlbot avatar Oct 30 '17 15:10 benlbot

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');
        });
      }
    });
  })();

BenjaminSchilller avatar May 19 '18 05:05 BenjaminSchilller