Re: [Django] #15881: FilteredSelectMultiple does not respect order

2023-09-14 Thread Django
#15881: FilteredSelectMultiple does not respect order
--+
 Reporter:  bmihelac  |Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  contrib.admin |  Version:  1.3
 Severity:  Normal|   Resolution:
 Keywords:  javascript| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  1
--+

Comment (by salmawisoky):

 The `FilteredSelectMultiple` widget in Django does not preserve the order
 of selected items by default. It displays the available choices in the
 order they are defined in the queryset, but the selected items are
 displayed in alphabetical order.

 If you want to maintain the order of selected items in the
 `FilteredSelectMultiple` widget, you can override the widget's rendering
 behavior by creating a custom widget that inherits from
 `FilteredSelectMultiple` and overrides the `render_option` method. [https
 ://drift-boss.pro Drift Boss]

 Here's an example of how you can create a custom widget that preserves the
 order of selected items:

 ```python
 from django.contrib.admin.widgets import FilteredSelectMultiple

 class OrderedFilteredSelectMultiple(FilteredSelectMultiple):
 def render_option(self, selected_choices, option_value, option_label):
 option_value = force_text(option_value)
 selected_html = (
 ' selected="selected"' if option_value in selected_choices
 else ''
 )
 return f'{force_text(option_label)}'
 ```

 In this custom widget, the `render_option` method is overridden to remove
 the alphabetical sorting behavior. Instead, it checks if the option value
 is in the selected choices and adds the `selected` attribute accordingly.
 This ensures that the selected items are rendered in the order they appear
 in the selected choices.

 To use this custom widget in your form, specify it as the widget for your
 `MultipleChoiceField`:

 ```python
 from django import forms

 class MyForm(forms.Form):
 my_field = forms.MultipleChoiceField(
 widget=OrderedFilteredSelectMultiple(attrs={'size': 10}),
 choices=my_choices,
 )
 ```

 Replace `my_field` with the name of your field and `my_choices` with the
 choices for your field.

 By using the `OrderedFilteredSelectMultiple` widget instead of the
 standard `FilteredSelectMultiple`, the selected items in the
 `FilteredSelectMultiple` widget will be displayed in the order they were
 selected.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a931839c0-163612c4-ff18-4516-aba6-f14afc488e5c-00%40eu-central-1.amazonses.com.


Re: [Django] #15881: FilteredSelectMultiple does not respect order

2016-01-27 Thread Django
#15881: FilteredSelectMultiple does not respect order
--+
 Reporter:  bmihelac  |Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  contrib.admin |  Version:  1.3
 Severity:  Normal|   Resolution:
 Keywords:  javascript| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  1
--+

Comment (by Pawamoy):

 It also does not respect the order of the selected choices when the form
 is reloaded.

 I am using a comma-separated-value-list custom field, which stores choices
 id in the form of a string (ex: '12,66,13').
 When retrieved from the database, this field is converted into a list
 using the split method. The example above would be converted to [12, 66,
 13].

 The first time you create the object with a new form, you can select
 choices one by one, and their order is kept in the resulting list (because
 they are added one by one in the widget right/bottom part by the
 javascript function). But when you save it and reload the page, the order
 is no longer maintained since it is lost while rendering the HTML. The
 selected part is now ordered the same way the available choices are.
 Indeed the javascript code just iterate over the "options" of the
 available part and add them in the chosen part if it encounters the
 "selected" attribute.

 The simplest solution I could think of to solve this is to add another
 attribute or class to the HTML "option" elements with the index of the
 selected choice as value, and use this new value in the javascript code to
 move them in the chosen part according to it.

 == Workaround ==

 === Python code in django.contrib.admin.widgets.Select.render_option ===

 {{{
 if option_value in selected_choices:
 selected_html = mark_safe(' selected="selected" order="%s"' % \
 selected_choices.index(option_value))
 }}}

 This would also require to have a list instead of a set (in
 render_options), because set are unordered:

 {{{
 # selected_choices = set(force_text(v) for v in selected_choices)
 selected_choices = list(force_text(v) for v in selected_choices)
 # Is set used to remove duplicates? If yes then
 # we need more code to remove them from the list too
 }}}

 === Generated HTML ===

 {{{
 
   Choice 1
   Choice 2
   Choice
 3
   Choice
 4
   Choice 5
   Choice
 6
 
 }}}

 === JavaScript code in SelectBox.js ===

 {{{
 move: function(from, to) {
 var from_box = document.getElementById(from);
 var to_box = document.getElementById(to);
 var option;
 var ordered_options = [];
 for (var i = 0; (option = from_box.options[i]); i++) {
 if (option.selected && SelectBox.cache_contains(from,
 option.value)) {
 ordered_options.push(option);
 }
 }

 ordered_options.sort(function(a, b) { return a.order - b.order; })

 for (var i = 0; i < ordered_options.length; i++) {
 SelectBox.add_to_cache(to, {
 value: ordered_options[i].value,
 text: ordered_ options[i].text, displayed: 1
 });
 SelectBox.delete_from_cache(from, ordered_options[i].value);
 }
 SelectBox.redisplay(from);
 SelectBox.redisplay(to);
 },
 }}}

 This is just a proposition, I didn't test it yet, but I'll post some
 results when I do.

--
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.309695c0b3f7b87acc9b381dce6b0d13%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #15881: FilteredSelectMultiple does not respect order

2011-04-21 Thread Django
#15881: FilteredSelectMultiple does not respect order
-+-
   Reporter:  bmihelac   |  Owner:  nobody
   Type: | Status:  new
  Cleanup/optimization   |  Component:  contrib.admin
  Milestone: |   Severity:  Normal
Version:  1.3|   Keywords:  javascript
 Resolution: |  Has patch:  0
   Triage Stage:  Accepted   |Needs tests:  0
Needs documentation:  0  |  Easy pickings:  0
Patch needs improvement:  0  |
-+-
Changes (by carljm):

 * needs_better_patch:   => 0
 * needs_docs:   => 0
 * needs_tests:   => 0
 * stage:  Unreviewed => Accepted


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.



[Django] #15881: FilteredSelectMultiple does not respect order

2011-04-21 Thread Django
#15881: FilteredSelectMultiple does not respect order
--+---
 Reporter:  bmihelac  |  Owner:  nobody
 Type:  Cleanup/optimization  | Status:  new
Milestone:|  Component:  contrib.admin
  Version:  1.3   |   Severity:  Normal
 Keywords:  javascript|   Triage Stage:  Unreviewed
Has patch:  0 |  Easy pickings:  0
--+---
 WIth filter_horizontal and filter_vertical admin options moving items
 between available and chosen, these items would be moved to the end of
 respecting lists.

 I believe it would be useful and not so hard to preserve original sort
 order.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.