{"id":6148,"date":"2014-04-13T04:34:48","date_gmt":"2014-04-13T04:34:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/sortable-table-columns-in-django-collection-of-common-programming-errors-2\/"},"modified":"2014-04-13T04:34:48","modified_gmt":"2014-04-13T04:34:48","slug":"sortable-table-columns-in-django-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/sortable-table-columns-in-django-collection-of-common-programming-errors-2\/","title":{"rendered":"Sortable table columns in django-Collection of common programming errors"},"content":{"rendered":"<p>I used the approach followed by the most voted answer above. But I solve the ascending\/descending ordering when clicking multiple times on the column header by using a custom tag.<\/p>\n<p>The tag:<\/p>\n<pre><code>@register.simple_tag\ndef url_replace(request, field, value):\n    dict_ = request.GET.copy()\n    if field == 'order_by' and field in dict_.keys():          \n      if dict_[field].startswith('-') and dict_[field].lstrip('-') == value:\n      # click twice on same column, revert ascending\/descending            \n        dict_[field] = value\n      else:\n        dict_[field] = \"-\"+value\n    else:\n      dict_[field] = value\n\n    return dict_.urlencode()\n<\/code><\/pre>\n<p>Then you use this tag on your column header, like above:<\/p>\n<th><code>Name<\/code><\/th>\n<p>First time you click it will sort in &#8216;descending&#8217; order, if you click the same header again it will revert to &#8216;ascending&#8217; order.<\/p>\n<p>This approach also preserves other parameters in your URL, like page number if you are using a paginator. It requires no extra libraries. Only thing you need to make sure is that your view is sending the RequestContext to the template.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I used the approach followed by the most voted answer above. But I solve the ascending\/descending ordering when clicking multiple times on the column header by using a custom tag. The tag: @register.simple_tag def url_replace(request, field, value): dict_ = request.GET.copy() if field == &#8216;order_by&#8217; and field in dict_.keys(): if dict_[field].startswith(&#8216;-&#8216;) and dict_[field].lstrip(&#8216;-&#8216;) == value: # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6148","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6148"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6148\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}