No JavaScript No Problem

[Infragistics] Murtaza Abdeali / Monday, November 7, 2011

In this day and age, it is rare that a user’s browser do not support JavaScript. But still, there are cases where sites are required to run on a browsers that do not support JavaScript, for accessibility purposes or for building sites that are more SEO friendly. You can still build such sites and continue to use NetAdvantage for jQuery product without having to maintain two different instances of your sites. I know what you are thinking, this doesn’t make sense, how can a fully JavaScript based toolset be used in a browser that do now support JavaScript, the answer lies on how you build up your sites. It is actually quite simple by just following a simple practice called progressive enhancement. You can first build your site for no JavaScript, and then light your page up by using Infragistics jQuery client widgets.

To demonstrate this example, I am going to show you examples of 3 different controls, grid, combo box & tree control. In all the three examples, I am going to show you how to take a HTML construct that more closely matches with what each control would look like, and then just by simply calling a wrapper on that construct, you can convert it into nicely styled, interactive jQuery controls.

jQuery Grid

Let’s begin with the grid first. As a web developer we all know what an HTML table looks like with table, head, header, rows & cells tags. In a browser where ever you want to display a table the contains data, it is a very standard thing to use the table element. In this example, I am going to convert a HTML table to an Infragistics jQuery Grid.

Let me now show you how I am converting a simple HTML table on the client to a jQuery grid with paging, filtering & sorting enabled. Here is what our rendered HTML table looks like, remember the id of the table, which is NorthWindCustomerInformation. We are going to use the id to grab the table element in scripting below.

   1: <table class="grid" cellspacing="0" rules="all" border="1" id="NorthWindCustomerInformation">
   2:    <thead>
   3:         <tr>
   4:             <th>Customer ID</th>
   5:             <th>Company Name</th>
   6:             <th>Contact Name</th>
   7:             <th>Contact Title</th>
   8:             <th>Street Address</th>
   9:             <th>City</th>
  10:             <th>Region</th>
  11:         </tr>
  12:    </thead>
  13:    <tbody>
  14:         <tr>
  15:             <td>ALFKI</td>
  16:             <td>Alfreds Futterkiste</td>
  17:             <td>Maria Anders</td>
  18:             <td>Sales Representative</td>
  19:             <td>Obere Str. 57</td>
  20:             <td>Berlin</td>
  21:             <td></td>
  22:         </tr><tr>
  23:             <td>ANATR</td>
  24:             <td>Ana Trujillo Emparedados y helados</td>
  25:             <td>Ana Trujillo</td>
  26:             <td>Owner</td>
  27:             <td>Avda. de la Constituci&#243;n 2222</td>
  28:             <td>M&#233;xico D.F.</td>
  29:             <td></td>
  30:         </tr><tr>
  31:             <td>ANTON</td>
  32:             <td>Antonio Moreno Taquer&#237;a</td>
  33:             <td>Antonio Moreno</td>
  34:             <td>Owner</td>
  35:             <td>Mataderos  2312</td>
  36:             <td>M&#233;xico D.F.</td>
  37:             <td></td>
  38:         </tr><tr>
  39:             <td>AROUT</td>
  40:             <td>Around the Horn</td>
  41:             <td>Thomas Hardy</td>
  42:             <td>Sales Representative</td>
  43:             <td>120 Hanover Sq.</td>
  44:             <td>London</td>
  45:             <td></td>
  46:         </tr><tr>
  47:             <td>BERGS</td>
  48:             <td>Berglunds snabbk&#246;p</td>
  49:             <td>Christina Berglund</td>
  50:             <td>Order Administrator</td>
  51:             <td>Berguvsv&#228;gen  8</td>
  52:             <td>Lule&#229;</td>
  53:             <td></td>
  54:         </tr><tr>
  55:             <td>BLAUS</td>
  56:             <td>Blauer See Delikatessen</td>
  57:             <td>Hanna Moos</td>
  58:             <td>Sales Representative</td>
  59:             <td>Forsterstr. 57</td>
  60:             <td>Mannheim</td>
  61:             <td></td>
  62:         </tr><tr>
  63:             <td>BLONP</td>
  64:             <td>Blondesddsl p&#232;re et fils</td>
  65:             <td>Fr&#233;d&#233;rique Citeaux</td>
  66:             <td>Marketing Manager</td>
  67:             <td>24, place Kl&#233;ber</td>
  68:             <td>Strasbourg</td>
  69:             <td></td>
  70:         </tr><tr>
  71:             <td>BOLID</td>
  72:             <td>B&#243;lido Comidas preparadas</td>
  73:             <td>Mart&#237;n Sommer</td>
  74:             <td>Owner</td>
  75:             <td>C/ Araquil, 67</td>
  76:             <td>Madrid</td>
  77:             <td></td>
  78:         </tr><tr>
  79:             <td>BONAP</td>
  80:             <td>Bon app'</td>
  81:             <td>Laurence Lebihan</td>
  82:             <td>Owner</td>
  83:             <td>12, rue des Bouchers</td>
  84:             <td>Marseille</td>
  85:             <td></td>
  86:         </tr><tr>
  87:             <td>BOTTM</td>
  88:             <td>Bottom-Dollar Markets</td>
  89:             <td>Elizabeth Lincoln</td>
  90:             <td>Accounting Manager</td>
  91:             <td>23 Tsawassen Blvd.</td>
  92:             <td>Tsawassen</td>
  93:             <td>BC</td>
  94:         </tr><tr>
  95:             <td>BSBEV</td>
  96:             <td>B's Beverages</td>
  97:             <td>Victoria Ashworth</td>
  98:             <td>Sales Representative</td>
  99:             <td>Fauntleroy Circus</td>
 100:             <td>London</td>
 101:             <td></td>
 102:         </tr><tr>
 103:             <td>CACTU</td>
 104:             <td>Cactus Comidas para llevar</td>
 105:             <td>Patricio Simpson</td>
 106:             <td>Sales Agent</td>
 107:             <td>Cerrito 333</td>
 108:             <td>Buenos Aires</td>
 109:             <td></td>
 110:         </tr><tr>
 111:             <td>CENTC</td>
 112:             <td>Centro comercial Moctezuma</td>
 113:             <td>Francisco Chang</td>
 114:             <td>Marketing Manager</td>
 115:             <td>Sierras de Granada 9993</td>
 116:             <td>M&#233;xico D.F.</td>
 117:             <td></td>
 118:         </tr><tr>
 119:             <td>CHOPS</td>
 120:             <td>Chop-suey Chinese</td>
 121:             <td>Yang Wang</td>
 122:             <td>Owner</td>
 123:             <td>Hauptstr. 29</td>
 124:             <td>Bern</td>
 125:             <td></td>
 126:         </tr><tr>
 127:              <td>COMMI</td>
 128:              <td>Com&#233;rcio Mineiro</td>
 129:              <td>Pedro Afonso</td>
 130:              <td>Sales Associate</td>
 131:              <td>Av. dos Lus&#237;adas, 23</td>
 132:              <td>Sao Paulo</td>
 133:              <td>SP</td>
 134:         </tr>
 135:     </tbody>
 136: </table>

Once we have the client HTML table, now in the script block of the page we can extract the table with its data in the client side data source component. After that, we can convert the table element into a jQuery grid. You can do that using the following code:

   1: <script>
   2:     $(function () {
   3:        var dataSource = new $.ig.DataSource({ dataSource: $("#NorthWindCustomerInformation")});
   4:         $("#NorthWindCustomerInformation").igGrid({
   5:             virtualization: false,
   6:             autoGenerateColumns: true,
   7:             dataSource: dataSource[0],
   8:             scrollbars: true,
   9:             height: '400px',
  10:             features: [
  11:                     {
  12:                         name: 'Paging',
  13:                         pageSize: 10, 
  14:                         type : 'local'
  15:                     },
  16:                     {
  17:                         name: 'Sorting',
  18:                         type: 'local'
  19:                     },
  20:                     {
  21:                         name: 'Filtering',
  22:                         type: 'local',
  23:                         filterDropDownItemIcons: false,
  24:                         filterDropDownWidth: 200
  25:                     }
  26:                 ]
  27:         });
  28:     });
  29: </script>

After doing so, the client HTML table will be converted to a grid control with support for paging, filtering & sorting.

jQuery Combo Box

Next, let’s take a look at converting a standard HTML select element to an Infragistics jQuery Combo box. Select elements come in handy when you want to provide choose from a list of item. In this section, I’ll convert a select element showing list of countries to a combo box.

To show you how we got from a select element to a combo box, I am going to start off with an HTML select box first to with options that will define countries we want to show in it.

   1: <select id="Countries">
   2:     <option>United States</option>
   3:     <option>United Kingdom</option>
   4:     <option>Bulgaria</option>
   5:     <option>Uruguay</option>
   6:     <option>Japan</option>
   7:     <option>India</option>
   8: </select>

Next, in our script block, we will grab this element using it’s id and call a jQuery plug-in on it. The plug-in we will use is the combo box, which will take the select element contents and convert it into jQuery Combo Box. Just one line of code is needed to do this, if you are not specifying any additional combo features.

   1: <script>
   2:     $(function () {
   3:         $("#Countries").igCombo();
   4:     });
   5: </script>

Once we do that, the select element is converted into a nicely styled combo box with drop down button and the same list of elements as before.

jQuery Tree

Lastly, let me show you one more example that I think will be very useful when developing applications that needs to run in browsers with no or minimum JavaScript capabilities. In this section, I am going to convert a UL/LI structure to a jQuery tree control with the ability to expand and collapse node. Within each LI, I will define an href, which will point that element to a URL. Upon converting the structure to a tree controls, you’ll see that the functionality will stay intact. Here is what we are going to do:

In order to do what is shown in the image above, first we need to define what my HTML structure, this could be something static on the page, or dynamically created on the fly to build up the navigation structure within applications. Let’s define our UL/LI structure as following on the page:

   1: <div id="InfoTree">
   2:     <ul>
   3:         <li>
   4:             Public Sites
   5:             <ul>
   6:                 <li>
   7:                     <a href="http://www.microsft.com">Microsoft</a>
   8:                 </li>
   9:                 <li>
  10:                     <a href="http://www.google.com">Google</a>
  11:                 </li>
  12:                 <li>
  13:                     <a href="http://www.apple.com">Apple</a>
  14:                 </li> 
  15:             </ul>
  16:         </li>
  17:         <li>
  18:             Infragistics Sites
  19:             <ul>
  20:                 <li>
  21:                     <a href="http://www.infragistics.com">Infragistics Site</a>
  22:                 </li>
  23:                 <li>
  24:                     <a href="http://samples.infragistics.com">Infragistics Samples</a>
  25:                 </li>
  26:                 <li>
  27:                     <a href="http://community.infragistics.com">Infragistics Community</a>
  28:                 </li> 
  29:                 <li>
  30:                     <a href="http://help.infragistics.com">Infragistics Help</a>
  31:                 </li>
  32:             </ul>
  33:         </li>
  34:     </ul>
  35: </div>

Upon having our UL/LI structure the way we wanted in our page, in the script block we need to grab the Div element that all this lives in and then call the jQuery tree plug-in on it. This will convert our entire mark-up defined above into a tree control. User can now expand and collapse the parent elements to show/hide the children nodes. You can also set other options on the tree as needed too.

   1: <script>
   2:     $(function () {
   3:         $("#InfoTree").igTree();
   4:     });
   5: </script>

Here we have it, by placing the snippet above the UL/LI mark-up will be converted into a jQuery client tree control.

Summary

There is too much excitement in the web development today, the flood gates have opened up with the capabilities you can bring to your applications. Browser with huge performance improvements to run JavaScript faster than ever before, HTML5 standards bringing RIA to native environment and the demand for unique and slick interfaces are continuing to raise the bar for what a web developer can deliver. Then, there is always a million dollar question we have to answer, what if the client is running a down-level browser, or can the site be made fully SEO friendly? Then we go back and forth with what an application might loose if it needs to support the backward technologies and hopefully come to a common ground. However, with the approach I showed above, you might be able to take a different approach and it gives you an additional option for building application that can support legacy stuff, but it can also light up on a modern browser.

Here are some additional resources on this topic that you might find helpful.