-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Example "Select2 Drag and Drop Sorting" in Select2 4.0 #3004
Comments
We have brought the old If you happen to know what elements need to exist and how the sorting should happen, I'd be interested in making a decorator for the |
Thanks for the quick reply. |
This mostly had to do with the fact that sorting a |
Selectize.js has a drag_drop plugin, but it requires JQuery UI. |
What is the correct way to add sortable to select2 4.0 ? |
If you're trying to use jQueryUI Sortable per the 3.5.3 instructions, this will make dragging work for Select2 v4: $("ul.select2-selection__rendered").sortable({
containment: 'parent'
}); As mentioned above by @kevin-brown the select2 'value' accessed via select2('val') is not yet wired to be aware of the order change. So until that is addressed you will need to roll your own 'sorted value getter', but it's doable via the dom. UPDATE: the sort order of the selections gets hosed when you add or remove from the selection. So maybe something can be done with the 'sorter' option... |
I was wondering whether there are plans to natively support the custom ordering of a multiple select component? |
Me too. Is there any example of this done? I'm having such a hard time trying to accomplish this... |
This comment #1190 (comment) offers a possible solution. You only have to make sure that the options to choose from are ordered in the correct way initially. |
okay this work, i used it like this (whit jqueryUI sortable installed and require.js):
and then
edit: my select2 is already created when i use this function, so no need to rebuild it.. |
@ambroiseRabier you're a lifesaver, thank you! I just had to remove the |
Method .first() does not accept any arguments.
|
I'm not using JQuery UI, anyway, I'm using this with JQuery EasyUI (a totally different beast!), so below is the drag'n'drop working with that. It's not the most elegant, and I had to do a few things after delays because easyui seems to throw in arbitrary animations every now and again that I needed to wait for them to end, but it certainly works well enough.
|
We don't have immediate plans to provide this. We are focused to fix some major UI bugs (that are majority of issues and PR's). But if you open a PR with unit tests, I will be glad to review and approve if everything is ok 👍 |
I rewrote the code from @ambroiseRabier .
})(jQuery); |
If your option label isn't the same as the ID, #3004 (comment) doesn't work. Here's what I used, for an already instantiated var selectEl = $('select').select2();
selectEl.next().children().children().children().sortable({
containment: 'parent', stop: function (event, ui) {
ui.item.parent().children('[title]').each(function () {
var title = $(this).attr('title');
var original = $( 'option:contains(' + title + ')', selectEl ).first();
original.detach();
selectEl.append(original)
});
selectEl.change();
}
}); |
Thank you @danielbachhuber with some tweaking this works! |
Small improvement to find the exact text
var selectEl = $('select').select2({width: '100%'}); selectEl.next().find('ul').sortable({ containment: 'parent', stop: function (event, ui) { ui.item.parent().children('[title]').each(function () { var title = $(this).attr('title'); $('option:contains(\'' + title + '\')', selectEl).filter(function(){ if ($(this).text() === title) { var original = $(this); original.detach(); selectEl.append(original); } }); }); selectEl.change(); } }); |
Thank you. it works for my case perfectly :))) |
Hello Select2 gods,
I'm looking for the "Select2 Drag and Drop Sorting" example in Select2 4.0.
It's not on the example page.
Is it still possible in 4.0?
The text was updated successfully, but these errors were encountered: