Using Infragistics jQuery Mobile Controls to Create a Mobile App

This post is written by guest blogger Damyan Petev

With the jQuery product, Infragistics envisioned business intelligence and data visualization across a range of platforms, not just one – more like every single one of them. Built on popular and largely supported HTML5 and jQuery UI and striving to provide a comprehensive toolset for development of performing and stylish applications that are to run on every device regardless of the OS. On top of that ‘mobile' was a subtle, yet dominant theme across our product line with the latest release. Touch support and metro themes everywhere!

But there’s more to it – the jQuery UI is awesome and will run on just about any modern browser, yet it was not specifically designed for mobile devices, not until recently anyway! jQuery’s Mobile framework is- still relatively fresh, but it is just what you might need – it’s touch optimized, it’s compatible with with most smartphones and tablets and other frameworks(like PhoneGap). It is designed to be lightweight and its UI controls designed for pocket-size pages and touch, but it’s not limited to that, as mobile pages will scale to the screen so it would work and look just as good on a bigger tablet and even a desktop(check out their demos using nothing but the framework). As with JQuery UI, it’s a strong cross-platform solution with a different perspective but the same goal – beautiful, powerful, universal solutions to your app problems. It sounds too good to miss, right? We thought so too…

Enter the Infragistics jQuery Mobile controls

With this release besides all controls taking on a touch-friendly nature, our NetAdvantage for jQuery family grew with specialized mobile controls leveraging the jQuery Mobile framework – the Mobile List View and a mobile version of the Rating. As explained, with them the focus is on mobile-centric and gesture-friendly UI. That means optimizations for mobile devices for stuff like better content placement and some specific interactions along with touch support – larger UI elements to interact with and specialized events like tap hold, swipes, etc.

The List view

Sometimes for small screen devices like smartphone, a full-fledged grid control would be somewhat hard to imagine and hardly optimal in terms of productivity. It’s why mobile design, while trying to fit as much functionality in a limited space as possible, must not clutter or expect to fit everything in one page. This is why the mobile framework provides AJAX based navigation between pages (and by pages I mean their mobile version and multiples can very well exist in one html file). This is also where a control such as the list comes in play – think of it as the mobile version of the grid type control – it fits the environment well, it’s easy to interact with, it can hold a lot of data and it can be hierarchical! The igListView is a widget that implements a hierarchical (nested) mobile list. Defining can be done in both in the well-known JavaScript/jQuery or using our ASP.NET (MVC) helpers. There’s also a third method that is also the default method for the mobile framework – a pure markup definition that also has some plus sides. Settings are made via the HTML “data-*” attributes that are mostly for data storage and ignored by browsers when interpreting UI. So for a list you can simple define the standard UL and LI elements and if for some reason jQuery scripts fail to load or execute the browser would still have valid UI to render.

Now that approach wouldn’t be very appropriate if you need some serious amount of data and the Infragistics Mobile ListView comes with a major data support enhancements, one of which is templating. That means not just controlling the markup of each record but also defining a single UL-LI element and point the widget to data and it will template it appropriately. The other major additions to data handling:

  • Search filtering with the added option of presets
  • Using the Infragistics Data Source means the controls can bind to a variety of sources
  • Sorting, sorting presets and groups separated with list deviders.
  • Load on Demand capabilities

iglistview_grouping_ios_default_1

The Rating

The mobile version of the rating control designed for touch means accuracy levels are restricted to whole numbers (or alternatively rounded to halves when the control is in read-only mode). The widget offers settings to control its layout along with getting and setting value, vote count and a changed value event along with the framework’s touch-oriented ones. And as expected you can always tweak the items the rating is using to some other shape or color.

igrating_ios_2

Style

As you can tell by the screenshots above we’ve designed a beautiful iOS style theme for our mobile controls. And for any of you familiar with jQuery UI you’d expect a common and powerful themeing framework to be in play for mobile as well – and there is! Our theme will style all default UI components as well and it can be replaced with one of your own. And yes, there’s a ThemeRoller for jQuery Mobile! It’s very easy to use – all mobile themes have 3 swatches that are usually used for default layouts and actions. Just like that our iOS theme has 3 swatches – a light C (default) swatch seen above with the list, darker B and the A swatch (the one with the exquisite fabric background above). I’ve made a theme myself for what was like 10 min in a metro-style attempt and I’ve included it in the demo just in case someone likes it:

iglistview_grouping_metro_3

 

A Mobile App

Let’s see how easy it can be to create a mobile app that is functional and stylish. We’ll stay on the wave of new technology for the demo and create this app using the new mobile template in ASP.NET MVC 4! What would that do is it would lay all the basic layout of the app for you – referencing all jQuery scripts (mobile included) and styles, bundling and minifying them. It would also render your Views in the mobile version of a page by marking the content DIV with data-role="page". So all left to do is add the Infragistics Mobile Loader ask for the control you need and define it. For this demo I’ll use Northwind again with two actions in the controller returning JSON serialized parts of it for an somewhat odd-styled manual load on demand scenario for the details of each list item (again this is different from the load on demand feature the igListView has which allows the end-user to request another portion of data to be loaded for his current list). Here’s the snippet for the list:

  1. <script>
  2.     $.ig.loader({
  3.         scriptPath: "../../Scripts/Infragistics-mobile/js/",
  4.         cssPath: "../../Content/Infragistics-mobile/css/",
  5.         resources: "igmList.Filtering.Sorting",
  6.         theme: "@ViewBag.Theme"
  7.     });
  8.  
  9. </script>
  10.  
  11. <ul id="actionFedList"
  12.     data-role="iglistview"
  13.  
  14.     data-auto-generate-layouts="false"
  15.     data-data-source="/Home/Customers"
  16.     data-bindings-header-key="ContactName"
  17.     data-bindings-primary-key="CustomerID"
  18.     data-bindings-text-key="Country"
  19.  
  20.     data-auto-generate-layouts="false"
  21.     data-initial-data-bind-depth="0"
  22.     data-bindings-details-title-key="CustomerID"
  23.  
  24.     data-sorting="true"
  25.     data-sorting-type="local"
  26.     data-sorting-auto-generate-sort-presets="false"
  27.     data-sorting-presets='[ {"text":"Customer ID","sortedFields":[ {"fieldName":"CustomerID","direction":"asc"} ]}, {"text":"Contact Name","sortedFields":[ {"fieldName":"ContactName","direction":"asc"} ]}, {"text":"Country","showGrouping":"true","sortedFields":[ {"fieldName":"Country","direction":"asc"} ]} ]'
  28.     
  29.     data-filtering="true"
  30.     data-filtering-type="local"
  31.     data-filtering-search-bar-field-name="ContactName,Country">
  32.     <li>              
  33.                     <ul data-role="childLayout"
  34.  
  35.                         data-auto-generate-layouts="false"
  36.                         data-data-source="/Home/Orders"
  37.                         data-bindings='{"customerId":"CustomerID"}'
  38.                         data-inset="true"
  39.                         data-generate-compact-jsonresponse="false"
  40.                         data-bindings-count-key="Freight"
  41.                         data-key="CustomerID"
  42.                         data-bindings-header-key="OrderID"
  43.                         data-bindings-text-key="ShipName"
  44.                         >
  45.                     </ul>
  46.                 </li>          
  47. </ul>

 

Note i have my theme set using the MVC dynamic ViewBag and I choose (and let users choose) a theme and call the controller action with that theme. Since I can’t put comments I’ve grouped code to be easier to see which part does what. The magic combo here is data-role="iglistview”. Also note the data source can be a link (it’s the igDataSource after all) so local JSON, actions results, services, XML, oData and so on – check! Defining header and text key are the properties in your data to be displayed for each record, the initial bind depth set to 0 will let the list know we have provided just the data for the main list (otherwise the list will ignore those layouts since child data is not loaded). Sorting and filtering are truly easy to enable with just a property and you can also customize them with presets and in them setting "showGrouping":"true causes the list to group the sorted results just like the screeenshots above. And yes you can define custom sorting functions and group comparers if you so desire, but for our basic demo there’s no need. In the child layout we again set the source to a actiong link and the data-bindings='{"customerId":"CustomerID"}' property instructs the list to include this as part of the links query so we can send the data for the right customer and the result:

iglistview_ios_details_4

This sub-page is all auto-generated for you, navigated to using AJAX, back button and history maintained!

Adding the rating is just as easy, again either through markup, helpers or initializing with script. Also as we mentioned you have the ability to now develop against specialized touch events so you can provide that little extra interaction to mobile users – we would use the long tap gesture to reset the rating and also handle the changed event:

  1. @using Infragistics.Web.Mvc
  2.  
  3. <script>
  4.     //load the resources:
  5.     $.ig.loader({
  6.         scriptPath: "../../Scripts/Infragistics-mobile/js/",
  7.         cssPath: "../../Content/Infragistics-mobile/css/",
  8.         resources: "igmRating"
  9.     });
  10.     // show the changed value
  11.     $(document).delegate("#rating", "igratingvaluechange", function (evt, ui) {
  12.         var voteCount = $(".selector").igRating("option", "voteCount");
  13.         $("#ratingValue").text("Rated: " + ui.value + " star(s)!");
  14.     });
  15.     // reset the rating on tap hol
  16.     $(document).bind("#rating", "taphold", function (evt, ui) {
  17.         if (!Modernizr.touch) {
  18.             return false;
  19.         }
  20.         $("#rating").igRating("value", 0);
  21.     });
  22. </script>
  23. <h2>
  24. some rating</h2>
  25. <P> Tap to set value, tap and hold to reset (zero). </P>
  26. <!-- Markup defined -->
  27. <div id="rating" data-role="igrating" data-value="3">
  28. </div>
  29.  
  30. <p id="ratingValue"></p>
  31.  
  32. <p> Read-only Rating defined with the MVC helper:</p>
  33.   @(Html.InfragisticsMobile().Rating()
  34.         .Value(4)
  35.         .ReadOnly(true)
  36.         .Render()
  37. )

You can get and set rating’s value and vote count at all times and the changed event offers both the new and old values along with regular references to owner and so on. Read-only mode rating converts to a smaller form (no longer need to be big enough to be touched):

igrating_readonly_ios_5

And, lastly, you can always override the default images or their locations to change the appearance of the control to match your theme. Below I’ve change the non-iOS default stars for the rating form yellow to blue to match my theme’s looks:

igrating_metro_6

Wrapping Up

If you want to build a mobile friendly app, the jQuery Mobile framework is an excellent choice and we are offering that extra functionality for you to take advantage of – hierarchical list with templating, advanced filtering, sorting, grouping and impressive load on demand features and support for a wide array of data sources and a beautiful mobile rating optimized for touch interactions to truly score you those five stars! And there’s a bonus – everything is optimized for mobile devices, but it looks and works perfectly good on a desktop as well!

I strongly recommend to take a closer look into jQuery Mobile and check out our awesome samples (there are codes in there for you to scan and try them on you mobile device too!). Also you can download a fully functional unlimited trial of NetAdvantage for jQuery and the Demo Project for this Blog – an APS.NET MVC 4 mobile application you’ve seen bits from above and should definitely try!






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

2 comments:

Anonymous said...

Has anyone tried the igtree control? I could not find a demo on how to use tooltips in that control

Edward Silva said...

Your post is so interesting. and it is more useful and helpful for me. Thanks for share this valuable post.