I have select2 multi select field in my form where I want to remove the selected option from the dropdown list after it is selected and again add it to the list if it is removed from the list. And also the added items should be in the same order as they selected. The current select2 (4.0) is not removing the selected the items and and it is showing the selected items in the order they appear in the drop down list, not in the order they are selected.
$(document).ready(function(){ $('#dynamicAttributes').select2({ allowClear: true, minimumResultsForSearch: -1, width: 600 }); });
JSFiddle: https://jsfiddle.net/rd62bhbm/
In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();
New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).
In order to change the selected option by the value attribute, all we have to do is change the value property of the <select> element. The select box will then update itself to reflect the state of this property.
Add Select All Option to jQuery Select2:First create the markup for the multi-value select box and a check box for selecting all option. Now add this java script to the page. The above script will be executed by onclick() event of the check box.
Part #1 of Q:
You can do a CSS trick to hide selected item
like this:
.select2-results__option[aria-selected=true] { display: none; }
Part #2 of Q:
Also you can do a JQuery trick to force selected items
to end of tags box, ( by getting selected item on select, detach it (remove it), then reAppend it to tags box, then call "change function" to apply changes ):
$("select").on("select2:select", function (evt) { var element = evt.params.data.element; var $element = $(element); $element.detach(); $(this).append($element); $(this).trigger("change"); });
Finally Updated JsFiddle, I hope it works for you, Thanks !
Edit #1
You can Clear All Selected
by this call (apply Null values):
$("#dynamicAttributes").val(null).trigger("change");
on Button:
$('#btnReset').click(function() { $("#dynamicAttributes").val(null).trigger("change"); });
Updated Fiddle #2
I find a way to make the selected values not to appear anymore on the selection pop up list
On the documentation you can they have list of events Select2 events
open
I make use of these select2 event open to hide the selected values
Here is the javascript ::
$(document).ready(function() { $('#dynamicAttributes').select2({ allowClear: true, minimumResultsForSearch: -1, width: 600 }); // override the select2 open event $('#dynamicAttributes').on('select2:open', function () { // get values of selected option var values = $(this).val(); // get the pop up selection var pop_up_selection = $('.select2-results__options'); if (values != null ) { // hide the selected values pop_up_selection.find("li[aria-selected=true]").hide(); } else { // show all the selection values pop_up_selection.find("li[aria-selected=true]").show(); } }); });
Here is a DEMO
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With