Change Themes Dynamically In Grid

In this post we will see how we can change themes dynamically in Grid. I have done this requirement by using some in-built functionalities of jQWidget JQX grid.

In this post we will see how we can change Themes Dynamically In Grid. I recently came across a situation to change the grid’s theme dynamically, whenever user select any theme. So I have done this requirement by using some in-built functionalities of jQWidget JQX grid. Here I am going to share that. I hope you will like it.

To load a grid from a JSON, you can follow the steps as discussed in this article.

Background

If you are new to JQWidget JQX Grid, please find out the link here.

Download the Source Code

Please download the source code from here.

Using the code

I hope you have implemented your grid as shown in that article. Now I guess your page will look like this.

Now let us make sure that grid is working fine. Please run your project.

Change Themes Dynamically In Grid
                                    Figure1: Change Themes Dynamically In Grid

Cool, so grid is loaded. Now need to add a select control in which we are going to load theme names so that user can select the theme and apply. Is that fine?

  1. <select id="selectOptions" onchange="applyNewCSS()">  
  2.     <option value="0">Select Template</option>  
  3.     <option value="metro">metro</option>  
  4.     <option value="office">office</option>  
  5.     <option value="orange">orange</option>  
  6.     <option value="energyblue">energyblue</option>  
  7.     <option value="darkblue">darkblue</option>  
  8.     <option value="shinyblack">shinyblack</option>  
  9.     <option value="lightness" selected="selected">lightness</option>  
  10.     <option value="android">android</option>  
  11. </select>   
The next thing is we need to add the CSS links to the page, here I am going to add some of the CSS references as follows.
  1. <link href="JQXItems/jqwidgets/styles/jqx.darkblue.css" rel="stylesheet" title="darkblue" />   
  2. <link href="JQXItems/jqwidgets/styles/jqx.energyblue.css" rel="stylesheet" title="energyblue" />   
  3. <link href="JQXItems/jqwidgets/styles/jqx.metro.css" rel="stylesheet" title="metro" />   
  4. <link href="JQXItems/jqwidgets/styles/jqx.metrodark.css" rel="stylesheet" title="metrodark" />   
  5. <link href="JQXItems/jqwidgets/styles/jqx.office.css" rel="stylesheet" title="office" />   
  6. <link href="JQXItems/jqwidgets/styles/jqx.ui-darkness.css" rel="stylesheet" title="ui-darkness" />   
  7. <link href="JQXItems/jqwidgets/styles/jqx.ui-le-frog.css" rel="stylesheet" title="ui-le-frog" />   
  8. <link href="JQXItems/jqwidgets/styles/jqx.ui-lightness.css" rel="stylesheet" title="ui-lightness." />   
  9. <link href="JQXItems/jqwidgets/styles/jqx.ui-sunny.css" rel="stylesheet" title="ui-sunny" />   
Have you noticed that we are calling the function applyNewCSS in the on change event of the select control. So next thing we will add that function. 
  1. function applyNewCSS() {  
  2.     var css = $.trim($("#selectOptions").val());  
  3.     if (css == 'metro') {  
  4.         $("#jqxgrid").jqxGrid({  
  5.             theme: 'metro';  
  6.         });  
  7.     } else if (css == 'metrodark') {  
  8.         $("#jqxgrid").jqxGrid({  
  9.             theme: 'metrodark';  
  10.         });  
  11.     } else if (css == 'office') {  
  12.         $("#jqxgrid").jqxGrid({  
  13.             theme: 'office';  
  14.         });  
  15.     } else if (css == 'orange') {  
  16.         $("#jqxgrid").jqxGrid({  
  17.             theme: 'orange';  
  18.         });  
  19.     } else if (css == 'energyblue') {  
  20.         $("#jqxgrid").jqxGrid({  
  21.             theme: 'energyblue';  
  22.         });  
  23.     } else if (css == 'darkblue') {  
  24.         $("#jqxgrid").jqxGrid({  
  25.             theme: 'darkblue';  
  26.         });  
  27.     } else if (css == 'black') {  
  28.         $("#jqxgrid").jqxGrid({  
  29.             theme: 'black';  
  30.         });  
  31.     } else if (css == 'shinyblack') {  
  32.         $("#jqxgrid").jqxGrid({  
  33.             theme: 'shinyblack';  
  34.         });  
  35.     } else if (css == 'lightness') {  
  36.         $("#jqxgrid").jqxGrid({  
  37.             theme: 'lightness';  
  38.         });  
  39.     } else if (css == 'ui-le-frog') {  
  40.         $("#jqxgrid").jqxGrid({  
  41.             theme: 'ui-le-frog';  
  42.         });  
  43.     } else if (css == 'ui-darkness') {  
  44.         $("#jqxgrid").jqxGrid({  
  45.             theme: 'ui-darkness';  
  46.         });  
  47.     } else if (css == 'ui-sunny') {  
  48.         $("#jqxgrid").jqxGrid({  
  49.             theme: 'ui-sunny';  
  50.         });  
  51.     } else if (css == 'android') {  
  52.         $("#jqxgrid").jqxGrid({  
  53.             theme: 'android';  
  54.         });  
  55.     }  
  56. }  
So we can add the theme to the grid as $(“#jqxgrid”).jqxGrid({ theme: ‘theme name’ });

Here we are applying the theme to the grid according to the user selection. Oops, we forgot one thing. If you apply CSS style sheet directly, the styles won’t get applied in the page content. For that we need to disable the remaining style sheets and enable the selected one alone.

The following code will do that magic:
  1. var i, link_tag;  
  2. for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length; i++)  
  3. {  
  4.     if ((link_tag[i].rel.indexOf("stylesheet") != -1) && link_tag[i].title)  
  5.     {  
  6.         link_tag[i].disabled = true;  
  7.         if (link_tag[i].title == css)  
  8.         {  
  9.             link_tag[i].disabled = false;  
  10.         }  
  11.     }  
  12.     set_cookie('style', css, 30);  
  13. }  
We are finding all the link reference and find out whose rel property has the word ‘stylesheet’. Now it is time to see how it woks.

Change Themes Dynamically In Grid
                                       Figure 2: Change Themes Dynamically In Grid

Change Themes Dynamically In Grid
                                      Figure 3: Change Themes Dynamically In Grid

You can see that the grid themes are changing according to the user selection of theme.That’s all we have done it.

Complete Code
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.     <head>  
  4.         <title>Change Themes Dynamically In Grid - Sibeesh Passion</title>  
  5.         <script src="jquery-1.9.1.js"></script>  
  6.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxcore.js"></script>  
  7.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxdata.js"></script>  
  8.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxbuttons.js"></script>  
  9.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxscrollbar.js"></script>  
  10.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxlistbox.js"></script>  
  11.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxdropdownlist.js"></script>  
  12.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxmenu.js"></script>  
  13.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.js"></script>  
  14.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.selection.js"></script>  
  15.         <script type="text/javascript" src="JQXItems/jqwidgets/jqxdatatable.js"></script>  
  16.         <link href="JQXItems/jqwidgets/styles/jqx.base.css" rel="stylesheet" />  
  17.         <link href="JQXItems/jqwidgets/styles/jqx.darkblue.css" rel="stylesheet" title="darkblue" />  
  18.         <link href="JQXItems/jqwidgets/styles/jqx.energyblue.css" rel="stylesheet" title="energyblue" />  
  19.         <link href="JQXItems/jqwidgets/styles/jqx.metro.css" rel="stylesheet" title="metro" />  
  20.         <link href="JQXItems/jqwidgets/styles/jqx.metrodark.css" rel="stylesheet" title="metrodark" />  
  21.         <link href="JQXItems/jqwidgets/styles/jqx.office.css" rel="stylesheet" title="office" />  
  22.         <link href="JQXItems/jqwidgets/styles/jqx.ui-darkness.css" rel="stylesheet" title="ui-darkness" />  
  23.         <link href="JQXItems/jqwidgets/styles/jqx.ui-le-frog.css" rel="stylesheet" title="ui-le-frog" />  
  24.         <link href="JQXItems/jqwidgets/styles/jqx.ui-lightness.css" rel="stylesheet" title="ui-lightness." />  
  25.         <link href="JQXItems/jqwidgets/styles/jqx.ui-sunny.css" rel="stylesheet" title="ui-sunny" />  
  26.         <script type="text/javascript">   

  27.         function applyNewCSS()   
  28.         {   
  29.            var css = $.trim($("#selectOptions").val());   
  30.            var i, link_tag;   
  31.            for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length; i++)   
  32.            {   
  33.               if ((link_tag[i].rel.indexOf("stylesheet") != -1) && link_tag[i].title)   
  34.                 {   
  35.                    link_tag[i].disabled = true;   
  36.                    if (link_tag[i].title == css)   
  37.                      {   
  38.                         link_tag[i].disabled = false;   
  39.                      }   
  40.                 }   
  41.            }   
  42.           if (css == 'metro')   
  43.              {   
  44.                 $("#jqxgrid").jqxGrid(   
  45.                 {   
  46.                    theme: 'metro'   
  47.                 });   
  48.              }   
  49.           else if (css == 'metrodark')   
  50.             {   
  51.                 $("#jqxgrid").jqxGrid(   
  52.                 {               
  53.                    theme: 'metrodark'   
  54.                 });   
  55.             }   
  56.           else if (css == 'office')   
  57.             {   
  58.                 $("#jqxgrid").jqxGrid(   
  59.                 {   
  60.                    theme: 'office'   
  61.                 });   
  62.             }   
  63.           else if (css == 'orange')   
  64.             {   
  65.                 $("#jqxgrid").jqxGrid(   
  66.                 {   
  67.                    theme: 'orange'   
  68.                 });   
  69.             }   
  70.           else if (css == 'energyblue')   
  71.             {   
  72.                 $("#jqxgrid").jqxGrid(   
  73.                 {   
  74.                    theme: 'energyblue'   
  75.                 });   
  76.             }   
  77.           else if (css == 'darkblue')   
  78.             {   
  79.                 $("#jqxgrid").jqxGrid(   
  80.                 {   
  81.                    theme: 'darkblue'   
  82.                 });   
  83.             }   
  84.           else if (css == 'black')   
  85.             {   
  86.                 $("#jqxgrid").jqxGrid(   
  87.                 {   
  88.                    theme: 'black'   
  89.                 });   
  90.             }   
  91.           else if (css == 'shinyblack')   
  92.             {   
  93.                 $("#jqxgrid").jqxGrid(   
  94.                 {   
  95.                    theme: 'shinyblack'   
  96.                 });   
  97.             }   
  98.           else if (css == 'lightness')   
  99.             {   
  100.                 $("#jqxgrid").jqxGrid(   
  101.                 {   
  102.                    theme: 'lightness'   
  103.                 });   
  104.             }   
  105.           else if (css == 'ui-le-frog')   
  106.             {   
  107.                 $("#jqxgrid").jqxGrid(   
  108.                 {   
  109.                    theme: 'ui-le-frog'   
  110.                 });   
  111.             }   
  112.           else if (css == 'ui-darkness')   
  113.             {   
  114.                 $("#jqxgrid").jqxGrid(   
  115.                 {   
  116.                    theme: 'ui-darkness'   
  117.                 });   
  118.             }   
  119.           else if (css == 'ui-sunny')   
  120.             {   
  121.                 $("#jqxgrid").jqxGrid(   
  122.                 {   
  123.                    theme: 'ui-sunny'   
  124.                 });   
  125.             }   
  126.           else if (css == 'android')   
  127.             {   
  128.                 $("#jqxgrid").jqxGrid(   
  129.                 {   
  130.                    theme: 'android'   
  131.                 });   
  132.             }   
  133.        }   
  134.      $(document).ready(function()   
  135.        {   
  136.           // prepare the data   
  137.           var data = {   
  138.           datatype: "json",   
  139.           datafields: [   
  140.           {   
  141.              "name""AreaCode",   
  142.              "type""string"   
  143.           },   
  144.           {   
  145.              "name""Revenue",   
  146.              "type""number"   
  147.           }],   
  148.               url: "jsonData.txt"   
  149.           };   
  150.           $("#jqxgrid").jqxGrid(   
  151.           {   
  152.              source: data,   
  153.              columns: [   
  154.              {   
  155.                 "text""Area Code",   
  156.                 "dataField""AreaCode",   
  157.                 "cellsalign""left",   
  158.                 "cellsformat""d"   
  159.              },   
  160.              {   
  161.                 "text""Revenue",   
  162.                 "dataField""Revenue",   
  163.                 "cellsalign""right",   
  164.                 "cellsformat""c2"   
  165.              }],   
  166.        });   
  167.     });   
  168.   
  169.         </script>  
  170.     </head>  
  171.     <body>  
  172.         <h3>Change Themes Dynamically In Grid - Sibeesh Passion</h3>  
  173.         <br />  
  174.         <div id='jqxWidget' style="float: left; width: 99.9%;">  
  175.             <div id="jqxgrid"></div>  
  176.         </div>  
  177.         <br />  
  178.         <br />  
  179.         <select id="selectOptions" onchange="applyNewCSS()">  
  180.             <option value="0">Select Template</option>  
  181.             <option value="metro">metro</option>  
  182.             <option value="office">office</option>  
  183.             <option value="orange">orange</option>  
  184.             <option value="energyblue">energyblue</option>  
  185.             <option value="darkblue">darkblue</option>  
  186.             <option value="shinyblack">shinyblack</option>  
  187.             <option value="lightness" selected="selected">lightness</option>  
  188.             <option value="android">android</option>  
  189.         </select>  
  190.     </body>  
  191. </html>   
Conclusion

Did I miss anything that you may think which is needed? Have you ever wanted to do this requirement? Did you try jQWidget yet? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Please see this article in my blog here.

Your turn. What do you think?

If you have any questions, then please mention it in the comments section.