Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set selected options after page refresh #11

Open
subdesign opened this issue Oct 27, 2017 · 2 comments
Open

set selected options after page refresh #11

subdesign opened this issue Oct 27, 2017 · 2 comments

Comments

@subdesign
Copy link

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'}
]
@benlbot
Copy link

benlbot commented Oct 30, 2017

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

@BenjaminSchilller
Copy link

BenjaminSchilller commented May 19, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants