Sortable table columns in django

Viewed 35227

I want to have sortable columns in my tables (just like what is done for the admin changelists)....I'm looking for a solution that will be easy to implement and customize if I want.

How can I do this?

5 Answers

If you use pagination rather than a Javascript table sorter, it might not be sufficient or behave unexpected.


Create every column header as a link e.g.

<th><a href="?order_by=name">Name</a></th>

and in your view you check whether the order_by parameter is set or not:

order_by = request.GET.get('order_by', 'defaultOrderField')
Model.objects.all().order_by(order_by)
Related