|
Combining persistent server side (ColdFusion) variables with client side (JQuery) values |
I stumbled upon an interested dilemma recently whilst building a search engine. The results of a search are returned, and can be viewed in a list or thumbnail view. The view toggle is a uses a JQuery function, so no persistent data is stored concerning a users current viewing option. The dilemma is how do you maintain the viewing mode across page reloads, such as for pagination, or filtering of the search results?
The views are both built, and toggled between using a JQuery function, like this:
2 $(document).ready(function() {
3 <cfif session.displaytype EQ "list">
4 $('##resultsgrid').hide();
5 <cfelse>
6 $('##resultslist').hide();
7 </cfif>
8
9 // toggles the search boxes on clicking the noted link
10 $('a##search-toggle').click(function() {
11
12 $('##resultslist').toggle(400);
13 $('##resultsgrid').toggle(400);
14
15 });
16 return false;
17 });
18 });
19</script>
This code will hide one of two div's based on a session variable. It also includes the code to toggle the div's with a link, like this:
The only way I could think of to persist the JQuery display selection was to store it as a server based session variable. I created a simple AJAX call and attached it to the toggle function, so that after each toggle the session variable was updated, like this:
2 type: "POST",
3 url: "changedisplay.cfm",
4 dataType: "html",
5 cache: false,
In the template 'changedisplay.cfm' I simply update the session value to the opposite of its current value.
2 <cfset session.displaytype = "thumb">
3<cfelse>
4 <cfset session.displaytype = "list">
5</cfif>
In this way the user can refresh or page through the result set and still maintain the same display. I'm really interested to see if anyone has done anything similar to this, or maybe taken a different angle.
So now to work out how to write cookies with javascript?
But it's not that hard to write your own Javascript to manage cookies. The web page I use as a resource when I want to manipulate cookies with Javascript is this one: http://www.quirksmode.org/js/cookies.html
I'd found that JQuery Cookie plug-in, its really good, a totally different direction than I would have gone, using Javascript to write your cookies.
I'll read up on the quirks mode article and see which way I prefer and write something up, thanks Brian!
One thing I learned was to use the CFParam tag to param the given cookie on the page you are referenceing. Just makes life easier.