jqGrid 3.5 with jQuery UI ThemeRoller Themes Demos

jqGrid 3.5 was released yesterday. It includes lots of good features, including a new rendering engine for faster loading. It also now supports jQuery UI ThemeRoller themes, so to start off itself you have access to variety of styles and can create your own easily too.

So I went ahead and created a demo that shows the grid in all these themes.
View Demo

How I did this style selection switch, follows

The creation of grid hasn't changed and all that script remains the same, I have blogged about it before. The main thing to look at here is using ColdFusion with jQuery to implement the style switching. There are probably others (and maybe better) ways to do this, but here I describe how I came about doing it.

The first thing to do was to make it scaleable, that means I don't have to know the names of the themes and don't have to change any code if a theme based on ThemeRoller is added in the future. We use good old cfdirectory for that and read all the ThemeRoller theme subdirectories from the css directory within our path. Then I build the form using cfselect and cfinput.

   view plainprintabout
 <!--- Get all the theme directories --->
 <cfdirectory action="list" directory="#GetDirectoryFromPath(GetTemplatePath())#css" type="dir" name="dirContents">
 <!--- Either the selected theme is passed in otherwise we set it to the first theme in directory --->
 <cfparam name="Form.cssName" default="#dirContents.name#">
 <!--- Our Form that displays the selections --->
 <cfform action="gridtest1.cfm" id="cssForm" name="cssForm">
 <table>
 <tr><td>Themes:</td><td>
 <cfselect query="dirContents" display="name" value="name" name="cssList" id="cssList" selected="#Form.cssName#">
10  </cfselect></td></tr>
11  <tr><td>
12  Current Theme:</td><td>
13  <cfinput type="text" id="cssName" name="cssName" value="#Form.cssName#">
14  </td></tr>
15  </table>
16  </cfform>
The form field csBane will be used to read the selected theme and load it. For that we have a short javascript function that loads a css file. I found this function on the net.
   view plainprintabout
 function load(url_, /*optional*/ media_)
 {
 // We are preventing loading a file already loaded
 var _links = document.getElementsByTagName("link");
 var _head = document.getElementsByTagName("head")[0];
 
 // Loop through the length of the links
 for( i = 0; _links.length >
i; i++)
 {
10   // If the href is already present, remove it
11   if (_links[i]["href"] == url_)
12   {
13   _head.removeChild(_links[i]);
14   }
15  }
16  
17  // Optional parameters check
18  var _media = media_ === undefined || media_ === null ? "all" : media_;
19  
20  // Build link element
21  var _elstyle = document.createElement("link");
22  _elstyle.setAttribute("rel", "stylesheet");
23  _elstyle.setAttribute("type", "text/css");
24  _elstyle.setAttribute("media", _media);
25  _elstyle.setAttribute("href", url_);
26  
27  // Add style
28  _head.appendChild(_elstyle);
29  }
Next we need a little bit of script in our main file to read the selected stylesheet and load it and also to submit our form when the style selection is changed.
   view plainprintabout
 <script>
 sel = document.cssForm.cssName.value;
 $("#cssList").change(function()
 {
     var selStyle = $("#cssForm option:selected").val();
     document.cssForm.cssName.value = selStyle;
     document.cssForm.submit();
 });
     var loadVar = "css/" + sel + "/jquery-ui-1.7.2.custom.css";
10      load(loadVar);
11  </script>
So here we read the selected theme and load it. We also bind the change function to our cfselect and set the cssName value to the selected option and submit the form.
Update: Demo is back Online.
Comments
John Allen's Gravatar I found your blog a couple of days ago. LOVE it.

Your writing is good and you write about what I want read.

Thanks
# Posted By John Allen | 8/2/09 6:15 PM
Kumar's Gravatar Thanks for the kind words John
# Posted By Kumar | 8/2/09 7:50 PM
Prashant Gupta's Gravatar You rock Kumar......
Your article just awesome...
# Posted By Prashant Gupta | 8/3/09 7:08 AM
Albert's Gravatar I found your blog today. this very good.
I cannot use cfgrid for some reason and I am trying your demo, but I have javscript error, I cannot dowload jqGrid 3.5 this link blocked.
Without these plugins it doesn't work.
Please help.

Thanks
# Posted By Albert | 8/11/09 12:45 PM
Kumar Shah's Gravatar Albert,

The jqGrid link should be working again, looks like they had some hosting problems.

As for cfgrid, when you use it make sure to turn of Request Debugging in ColdFusion Administrator.
# Posted By Kumar Shah | 8/11/09 12:49 PM
Albert's Gravatar Dear Kumar,
Thanks for quick response,
I have developed very siplme code for our purpose,
I've get data from cfdirectory and put result into cfgrid,
It works on my PC fine, but it doesn't on production unix box.
I have RSL failed to load error, that why
I am trying to use your latest jqGrid
# Posted By Albert | 8/11/09 12:59 PM
Kumar Shah's Gravatar @Albert
That sounds like an error with the with the ColdFusion setup. Google tells me it has something to do with not granting access to CFIDE/scripts/ directory.
# Posted By Kumar Shah | 8/11/09 3:23 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.003.  Design based on ARCLITE by: digitalnature