Two synchronized Selects using bsmultiselect

Viewed 106

that's me again :)

I've been wondering how to make two synchronized bsmultiselect using the html tag "select" instead of a div, since I need to send the information via form. Here's the code. What i want is to make it a "select" instead of a "div"

var options = [

  { text: 'item1', value: 'item1', selected: true},
  { text: 'item2', value: 'item2', selected: false},
  
];

function isValidSelection(v) {
    return true;
    // RP: simplified since sensce of current filter is unclear
    /*var l = options.filter(function (item) {
        return item.selected == true;
    }).length;
    var r = (!v) || (v && l == 0);
    return r;*/
}

var classeGramatical1 = null;
var classeGramatical2 = null;
let isclasseGramatical2 = true;
var $classesGramaticais = $('.classeGramatical1')
if (isclasseGramatical2)
    $classesGramaticais = $classesGramaticais.add('.classeGramatical2')

var install = function () {
    $classesGramaticais.bsMultiSelect({
        options: options,
        setSelected: function (o, v) {
            if (isValidSelection(v)) {
                o.selected = v;
                return true;
            }
            else {
                return false;
            }
        }
    });
    if (isclasseGramatical2) {
        $('.classeGramatical1').on('dashboardcode.multiselect:change', function () {
            $('.classeGramatical2').bsMultiSelect("UpdateOptionsSelected");
        })
    }

}
install();
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@dashboardcode/bsmultiselect@0.6.28/dist/js/BsMultiSelect.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<label class="control-label" for="Classe">Classe:</label>
<div class="classeGramatical1" multiple="multiple"required></div>

<label class="control-label" for="Classe">Classe:</label>
<div class="classeGramatical2" multiple="multiple"required></div>

1 Answers

In case you need two <select> be sent with form:

  1. Create two selects, and two bsMultiSelects over the them
  2. manipulate data on setSelected- set <option> selected attribute (or create new options)
  3. push changes to the relatedBsMultiSelect (with UpdateOptionsSelected ir UpdateData or other pushers)

In case you need one <select> be sent with form (and second bsMultiSelect is used only for some data manipaulations):

  1. create one bsMultiSelect over the <select> and second over the div 1b) the second one initiate with configuration {options: $('#mySelect < option').toArray() }
  2. all the same: change data, then push changes. if you change own bsMultiSelect data (not other one) push changes with UpdateOptionsSelected in the new events loop window.setTimeout(,0);
Related