I've got an HTML table that lists a bunch of entries. I want to make the column headers clickable to be sorted. Problem is, I already have a bunch of filters in the URL (stuff like ?min_price=200&max_price=6000
). How can I build the link such that it keeps these params in tact? Is there a function for it? Do I have to do it in the view?
views:
25answers:
2
+1
A:
Here's a cute trick I found that might do what you want: http://andrewwilkinson.wordpress.com/2009/02/03/using-django-forms-for-get-urls/
Personally, I wouldn't do it that way. I'd use Javascript and change the DOM directly. You're not adding or removing any data if you are just sorting. If you just change the DOM you'll possibly avoid calling the server, and the user experience will be better because you won't need to reload the screen.
jfenwick
2010-08-19 17:39:36
Except that it's multi-page. JS won't work.
Mark
2010-08-19 17:53:06
Also...the link you provided.. I'm not sure how URL-encoding a form is even relevant? If what you really mean to say is "copy the GET dict, add in your sort param, and then use urllib to urlencode it" then maybe you have an answer.
Mark
2010-08-19 17:56:25
Have you seen jqgrid?http://www.trirand.com/blog/It's a fairly sophisticated grid widget.It has dynamic pagination and sorting.
jfenwick
2010-08-19 18:01:50
@jfenwick: Not looking for a full-featured spreadsheet...just a simple table. Some cells are more complicated, containing images, or multiple pieces of data.
Mark
2010-08-19 21:32:01
+1
A:
In your template, you can add:
your_current_url?{{ request.META.QUERY_STRING }}
to pass the current query string params on to additional links.
Hope that helps!
Matthew J Morrison
2010-08-19 17:41:32
...assuming of course that you're using the request context processer...
Matthew J Morrison
2010-08-19 17:43:09
Nice. I learned something today :)Also, look out for this when using GET:http://stackoverflow.com/questions/266322/http-uri-get-limit
jfenwick
2010-08-19 17:47:54
I thought it was implicit, but I guess not. Clicking the headers would add something like a `sort_by` param. With your solution, clicking one header, and then another would just keep adding `sort_by` params. It needs to be replaced so there are no dupes.
Mark
2010-08-19 17:52:21
in that case you could write a template tag to pull what you want, or exclude what you don't want from request.META['QUERY_STRING'].
Matthew J Morrison
2010-08-19 18:02:13
It still doesn't make sense to parse `request.META['QUERY_STRING']` when everything I need is already in `request.GET`, which can easily be `urlencoded` and will automatically prevent dupes given its a dict.
Mark
2010-08-19 21:30:30
right, sorry, that's what I meant. request.GET so you don't have to parse anything, good catch.
Matthew J Morrison
2010-08-19 22:56:31