Multi-column combo box – NetAdvantage for jQuery

Atanas Dyulgerov / Friday, January 13, 2012

In this article I will show you how to turn the Infragistics’ jQuery combo box into a multi-column combo box. This would be useful if you want to display the values of more than just one property of each item in the dropdown list of the combo box. Using this design pattern you could simplify the life of your users by providing them with a more informative and descriptive way to choose items from a list.

multicolumn combobox

Straight away I should say that there is no out of the box functionality for this and what I would actually do is show you how to make it look like a multi column combo box. This will be done by creating a few templates and styles. It is straight forward.

The first thing that we need to do is set up the environment in which we’ll create the control. That including the required JS scripts and CSS files. We need the jQuery, the jQuery UI and jQuery Templates scripts. In addition we need the IG UI base and IG UI Combo JS files. The easiest way to start using them is to set links to the respective CDN locations. Here is how to do that for an HTML page.

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
  2. <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js" type="text/javascript"></script>
  3. <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"></script>
  4. <script src="http://cdn-na.infragistics.com/jquery/20112/2045/js/combined/min/ig.ui.min.js" type="text/javascript"></script>
  5. <script src="http://cdn-na.infragistics.com/jquery/20112/2045/js/combined/min/ig.ui.combo.min.js" type="text/javascript"></script>

You also need some basic css styles to get the default look of the Infragistics combo box. Again for an HTML page here is what you need to include

  1. <link href="http://cdn-na.infragistics.com/jquery/20112/2045/themes/min/ig/jquery.ui.custom.min.css" rel="stylesheet" type="text/css" />
  2. <link href="http://cdn-na.infragistics.com/jquery/20112/2045/themes/min/base/ig.ui.min.css" rel="stylesheet" type="text/css" />

Note that the URL for the Infragistics CDN includes format of the type …jquery/xxxxx/yyyy/js…. the xxxxx is the release version, 20112 is the latest as of the time of writing this article. The other number is the build that you want to use. By the time you read this there might be a newer version and you will need to change that number if you want to use it instead. Note also that all those files come with the product download and you are not required to use the CDN.

Once we have all this in place we can add a div that will be our combo box and work on it. I’ll set its id to comboNetflix, as I’ll display data from the Netflix’s movie database (an open free data service with oData protocol).

After I add the div I ‘m going to include a short script tag that will convert it to an igCombo control and will set the appropriate properties. Lastly I’ll create some custom css settings too. You’ll be able to see the code a little further down the article.

So before I show you the specifics I will explain how to create the separate columns in theory. The combo box does not have them by default out of the box, but the drop down list is structured in a manageable way. We have a header space at the top, content in the middle and footer at the bottom. The content is a list of items. All those elements, the header, the items and the footer are template-able. They can contain any HTML elements as a template. This feature works only if we have included the jQuery template pack. So what we will do is provide a template that will make the items look like in a table and each column will show the value for a property of the respective item. I will implement this by using spans with predefined widths, borders and margins. The end result will look exactly like the screenshot in the beginning of the article.

So in the body of the page we add this:

  1. <body>
  2.     <div id="container">
  3.         <div id="comboNetflix" />
  4.     </div>
  5. </body>

The container div will only be used to position the combo in the middle of the screen and is not really needed to this sample. It just makes it look a little better.

Then in the head section I’ll add the following script:

  1. <script type="text/javascript">
  2.     $(function () {
  3.         $("#comboNetflix").igCombo({
  4.             filteringType: "local",
  5.             renderMatchItems: "contains",
  6.             responseDataKey: "d.results",
  7.             valueKey: "Name",
  8.             width: "530px",
  9.             dataSource: "http://odata.netflix.com/v2/Catalog/Genres('Documentaries')/Titles?$inlinecount=allpages&$top=10&$filter=substringof('experience',Name)%20eq%20true&$format=json&$callback=?",
  10.             headerTemplate: "<div id='header'><span class='cell first'>Name</span><span class='cell middle'>Year</span><span class='cell last'>Rating</span></div>",
  11.             itemTemplate: "<span class='cell first'>${ShortName}</span><span class='cell middle'>${ReleaseYear}</span><span class='cell last'>${AverageRating}</span>",
  12.             footerTemplate: "<p style='margin:0px; padding: 5px; margin-left:6px; font-weight: bold'>Data taken from odata.netflix.com.</p>'
  13.         });
  14.     });
  15.  
  16. </script>

Lets take a closer look at this part. We select the div that will be our future combo and call the Infragistics method igCombo(). The parameters then tell it how it will look like and behave. Here is what is interesting specifically for the multi-column combo. The valueKey is the name of the property that will be used when an item is selected to fill in the combo box value. Width is the actual width of the future combo box. It will override the existing div CSS styles if you have them. Then the data source is a standard oData service with some filtering parameters. It returns result in JSON format. And now the most interesting properties – templates for the header, footer and items. Those can be anything. They can be image tags, text, tables, whatever you want that HTML allows. As I said before I’ll have three spans next to each other for every line with some CSS styling that I will show you in a second. The content in each cell is dynamic. You can reference the content of properties of each item by using this syntax: ${PropertyName}. Also note that the footer will actually be separate. What you will make of it is completely up to you.

In the CSS I have a few classes. The cell class will be used for each span in the header and content. It will set the horizontal borders and some padding that is common for all cells. Also it will set the float to left so they are aligned properly. Then the class first will set the width for the first column and provide a right border and some margin to the left. Similarly the middle and last classes will customize the other columns. I will also have a section that makes the header cells bold and the border under the header solid instead of dotted. Here is the complete CSS:

  1. <style type="text/css">
  2.     #container
  3.     {
  4.         position: absolute;
  5.         left: 50%;
  6.         margin-left: -275px;
  7.         top: 25%;
  8.     }
  9.     #header
  10.     {
  11.         margin-left: 3px;
  12.         margin-top: 5px;
  13.     }
  14.     #header .cell
  15.     {
  16.         font-weight: bold;
  17.         border-bottom: 1px solid black;
  18.     }
  19.     .cell
  20.     {
  21.         float: left;
  22.         border-bottom: 1px dotted black;
  23.         padding: 5px;
  24.     }
  25.     .first
  26.     {
  27.         width: 330px;
  28.         border-right: 1px dotted black;
  29.         margin-left: 5px;
  30.     }
  31.     .middle
  32.     {
  33.         width: 60px;
  34.         border-right: 1px dotted black;
  35.     }
  36.     .last
  37.     {
  38.         width: 70px;
  39.         margin-right: 5px;
  40.     }
  41. </style>

Note again that the container is not related to the multicolumn combo box its just there to put it in the middle of the screen.

So that’s basically all you need – include the proper JS and CSS files, add an empty div that will be constructed as the igCombo by a script with some properties in the head section and lastly the header, items and footer of the combo will be templated by providing some HTML blocks to the respective properties and styling them with CSS.

I hope this has been useful and interesting. Here is a link to the complete sample that I just showed.