diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 366577519..5ee37563c 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -276,7 +276,8 @@ class OrderingFilter(BaseFilterBackend): def get_template_context(self, request, queryset, view): current = self.get_ordering(request, queryset, view) - current = None if not current else current[0] + if not current: + current = None options = [] context = { 'request': request, @@ -284,11 +285,36 @@ class OrderingFilter(BaseFilterBackend): 'param': self.ordering_param, } for key, label in self.get_valid_fields(queryset, view, context): - options.append((key, '%s - %s' % (label, _('ascending')))) - options.append(('-' + key, '%s - %s' % (label, _('descending')))) + options.append((key, '%s - %s' % (label, _('ascending')), *self.plus_key(current, key))) + options.append(('-' + key, '%s - %s' % (label, _('descending')), *self.plus_key(current, '-' + key))) context['options'] = options return context + @staticmethod + def plus_key(current, key): + priority = None + plus_key = None + if current: + if len(current) > 1: + try: + priority = current.index(key) + 1 + except ValueError: + pass + + for_plus = current.copy() + if key in for_plus: + # for click on minus + for_plus.remove(key) + else: + # for click on plus - rearrange sort priorities + # remove key or -key + for_plus = [k for k in for_plus if k.lstrip('-') != key.lstrip('-')] + for_plus.append(key) + if for_plus: + plus_key = ','.join(for_plus) + + return priority, plus_key + def to_html(self, request, queryset, view): template = loader.get_template(self.template) context = self.get_template_context(request, queryset, view) diff --git a/rest_framework/static/rest_framework/css/default.css b/rest_framework/static/rest_framework/css/default.css index 51ca3ba19..99fca1f15 100644 --- a/rest_framework/static/rest_framework/css/default.css +++ b/rest_framework/static/rest_framework/css/default.css @@ -80,3 +80,11 @@ pre { #filtersModal .modal-body h2 { margin-top: 0 } + +#filtersModal .list-group-item .glyphicon-plus[data-plus-ordering] { + visibility: hidden; +} + +#filtersModal .list-group-item:hover .glyphicon-plus[data-plus-ordering] { + visibility: visible; +} \ No newline at end of file diff --git a/rest_framework/static/rest_framework/js/default.js b/rest_framework/static/rest_framework/js/default.js index bec2e4f9e..b7c5aa32e 100644 --- a/rest_framework/static/rest_framework/js/default.js +++ b/rest_framework/static/rest_framework/js/default.js @@ -41,6 +41,12 @@ $(document).ready(function() { $('.form-switcher a:first').tab('show'); } + // add onclick to ordering plus and minus glyph + $('#filtersModal span[data-plus-ordering]').click(function(e) { + var glyph = $(e.target); + glyph.closest('a.list-group-item').attr('href', glyph.attr('data-plus-ordering')); + }); + $(window).on('load', function() { $('#errorModal').modal('show'); }); diff --git a/rest_framework/templates/rest_framework/filters/ordering.html b/rest_framework/templates/rest_framework/filters/ordering.html index b71b2a5bf..e094c6ce9 100644 --- a/rest_framework/templates/rest_framework/filters/ordering.html +++ b/rest_framework/templates/rest_framework/filters/ordering.html @@ -2,13 +2,24 @@ {% load i18n %}