Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Multiple Select Control Sortable

Does anyone know how to make a multiple select (see below HTML) sortable? Using either jQuery or Ext JS? By sortable, I mean that I can drag an item (an option) up or down to reposition it in the select control.

<select id="testing" multiple=multiple>
    <option>First Option</option>
    <option>Second Option</option>
    <option>Third Option</option>
    <option>Forth Option</option>
    <option>5 Option</option>
    <option>6 Option</option>
    <option>7 Option</option>
</select>
like image 592
Moses Ting Avatar asked Aug 11 '26 08:08

Moses Ting


2 Answers

✅✅ selectize/selectize.js

This can be attached to text (freetagging) as well as to select. I does have drag_drop and remove_button plugins.

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
<script>
$(function() {
    $('select').selectize({
      plugins: ['remove_button', 'drag_drop']
  });
});
</script><select multiple>
<option value ="1">One</option>
<option value ="2" selected>Two</option>
<option value ="3" selected>Three</option>
</select>

✅ jshjohnson/Choices

This can be attached to a select element and has an ordered option which then rewrites the order of the select options. So your selections are returned in the order you select them. Drag-and-drop is not possible though and will not be implemented.

Other libraries that show the order of selection in the UI but do not allow drag-and-drop nor seem to change the select accordingly

  • Dropdown | Semantic UI (1, 2)
  • jQuery Dropdown | Dane
  • Fastselect
  • Light Weight MultiSelect Test Page

Other select libraries that sort their options and do not respect ordering in the first place

  • tail.select
  • jQuery Pretty Dropdowns Demo
  • creativecirclemedia/dropdown-checkboxes
  • jQuery Selectric
  • Jquery.sumoselect by Hemant Negi

Unknown but unlikely

  • jQuery Selectpick
  • https://prevwong.github.io/awesome-select/
  • PebbleRoad/combo-select
  • zeusdeux/fuzzy-dropdown
  • ddSlick
  • Hierarchy Select jQuery Plugin for Twitter Bootstrap 4
  • BenMMcLean/BetterSelecter
  • bschueller/jquery.selectMe
like image 191
geek-merlin Avatar answered Aug 13 '26 23:08

geek-merlin


You can't do it reliably with drag-and-drop. <select multiple> may be implemented using an OS select-list widget, which will not typically generate mouse events like the native HTML elements do.

You'd have to replace the <select> with a load of <div> analogues for it, which do provide sortability in the normal way whichever library/plugin you want to use for it does. It's a fair bit of bother to make an ersatz-<select> nicely accessible/usable/keyboardable though; most plugins don't really manage to fully.

The simpler alternative would be simply to have separate ‘Move up’/‘Move down’ buttons, which find and move the selected <option>​s in the element.

like image 36
bobince Avatar answered Aug 13 '26 21:08

bobince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!