Shaun Mccran

My digital playground

26
F
E
B
2010

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:

view plain print about
1<s/cript language="javascript">
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:

view plain print about
1<a href="javascript:void(0);" id="search-toggle">Toggle display</a>

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:

view plain print about
1$.ajax({
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.

view plain print about
1<cfif session.displaytype EQ "list">
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.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Brian Swartzfager's Gravatar Another option would be to preserve the state in a cookie in the browser. The dataTables jQuery plugin you blogged about the other day, for example, does exactly that when you make use of the bStateSave option it has.
# Posted By Brian Swartzfager | 26/02/2010 19:15
Shaun McCran's Gravatar Thats an interesting point, I hadn't considered the cookie scope, I must admit I don't really use it much. From what you've said above it sounds like JQuery can be used to write cookies, so I could loose the AJAX http request altogether.

So now to work out how to write cookies with javascript?
# Posted By Shaun McCran | 27/02/2010 15:12
Brian Swartzfager's Gravatar @Shaun: There's at least one jQuery plugin (I'm sure there are more) for reading and writing cookies: http://plugins.jquery.com/project/cookie.

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
# Posted By Brian Swartzfager | 27/02/2010 21:19
Shaun McCran's Gravatar Hi,
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!
# Posted By Shaun McCran | 28/02/2010 21:50
Ben Nadel's Gravatar I second @Brian's suggestions. In application, I will sometimes store non-critical user data in cookies to remember things like the last TAB a user had opened. It works very nicely and requires very little update to the CFML.

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.
# Posted By Ben Nadel | 01/03/2010 21:03
Back to top