tags:

views:

25

answers:

2

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?

+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
Except that it's multi-page. JS won't work.
Mark
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
Have you seen jqgrid?http://www.trirand.com/blog/It's a fairly sophisticated grid widget.It has dynamic pagination and sorting.
jfenwick
@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
+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
...assuming of course that you're using the request context processer...
Matthew J Morrison
Nice. I learned something today :)Also, look out for this when using GET:http://stackoverflow.com/questions/266322/http-uri-get-limit
jfenwick
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
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
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
right, sorry, that's what I meant. request.GET so you don't have to parse anything, good catch.
Matthew J Morrison