Juice UI – Microsoft embraces jQuery UI

Juice UI
I couldn’t be more excited to announce our involvement in Juice UI. Juice UI is appendTo’s modern equivalent to the Ajax Control Toolkit. Just like we realized with Wijmo, appendTo decided that jQuery UI was a great foundation to build on in JavaScript. They have fully adopted jQuery UI by making ASP.NET Controls and Extenders for the widgets shipped with jQuery UI. Juice UI is developed by our friends at appendTo. They have done a great job at making an ASP.NET version of jQuery UI. Now jQuery UI is more accessible to .NET developers that are not yet familiar with JavaScript.

Read more about our involvement in Juice UI.

And keep an eye out for something we have coming soon…

Wijmo Wednesday: Server Side Grid Magic

In a previous post, we discussed how to add a whole bunch of awesome features to a standard HTML table and transform it into a “grid”.  What I’d like to do today is show you how to move a lot of this functionality over to the server.

By harnessing the server to do a lot of the “heavy lifting”, we can give our users a smooth experience.  What heavy lifting would need to be done?  Assume you have a data set of 1 million rows.  That’s a lot of data to send to the client, and then sort/page/filter.  If we ask the server to sort/page/filter the data before sending it to the client, we can quickly reduce the overhead of the client.

I’m going to be using the example from my webcast, which you can access here.

The Server

For data, I’m using the Chinook database.  It’s quick and easy to set up, and gives me good data to use in my examples.  I’m using ASP.NET MVC 3 for my examples, and we’ll only be implementing sorting and filtering.

        public JsonResult GetAlbumList()
        {
            int pageSize = Request.Params["paging[pageSize]"] != null ? Convert.ToInt32(Request.Params["paging[pageSize]"]) : 0;
            int pageIndex = Request.Params["paging[pageIndex]"] != null ? Convert.ToInt32(Request.Params["paging[pageIndex]"]) : 0;

            string sortColumn = Request.Params["sorting[0][dataKey]"];
            string sortDirection = Request.Params["sorting[0][sortDirection]"];

            if (string.IsNullOrEmpty(sortColumn)) sortColumn = String.Empty;
            if (string.IsNullOrEmpty(sortDirection)) sortDirection = String.Empty;

            using (var entity = new ChinookEntities())
            {
                var allAlbums = from al in entity.Albums
                                join ar in entity.Artists on al.ArtistId equals ar.ArtistId
                                select new AlbumResult()
                                           {
                                               AlbumName = al.Title,
                                               ArtistName = ar.Name
                                           };

                var totalRowCount = allAlbums.Count();

                if (pageSize == 0)
                    pageSize = totalRowCount;

                if (sortColumn.ToLower() != "album")
                    allAlbums = sortDirection.ToLower() == "descending"
                                    ? allAlbums.OrderByDescending(p => p.ArtistName).Skip(pageSize*pageIndex).Take(pageSize)
                                    : allAlbums.OrderBy(p => p.ArtistName).Skip(pageSize*pageIndex).Take(pageSize);
                else
                    allAlbums = sortDirection.ToLower() == "descending"
                                    ? allAlbums.OrderByDescending(p => p.AlbumName).Skip(pageSize*pageIndex).Take(pageSize)
                                    : allAlbums.OrderBy(p => p.AlbumName).Skip(pageSize*pageIndex).Take(pageSize);

                var result = new WijmoGridResult { Items = allAlbums.ToList(), TotalRowCount = totalRowCount };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

Let’s walkthrough what this code does, because it does a lot.  The first six lines pulls out Request parameters that Wijmo will send.  Because it is possible for these values to be null, a little bit of self checking needs to take place.

Starting at the using statement, we’re opening a connection to our database with Entity Framework.  We use the idea of lazy loading to create a structured query so our round trip to SQL Server only happens once.  The first query creates a query for all the albums in the database.  This is needed to get the total row count.  Wijmo will use that to determine the paging requirements.

For sorting, you’ll probably be drawn to the epic IF..ELSE statement.  All this is doing is determining what column we want to sort by and how we want to sort it (ascending or descending), and apply the correct LINQ filter.  These calls are also using the Skip() and Take() technique for pagination. 

Finally, you might notice I don’t have the definitions for AlbumResult and WijmoGridResult.  Here is the code for those classes:

    public class AlbumResult
    {
        public string AlbumName { get; set; }
        public string ArtistName { get; set; }
    }

    public class WijmoGridResult
    {
        public List Items { get; set; }
        public int TotalRowCount { get; set; }
    }

The Client

Now that the server is configured, we need to change our Wijmo configuration a little bit to use the new settings.

	$(document).ready(function () {

		var dataReader = new wijarrayreader([
				{ name: "Artist", mapping: "ArtistName" },
				{ name: "Album", mapping: "AlbumName" }
			]);

		var dataSource = new wijdatasource({
			proxy: new wijhttpproxy({
				url: "@Url.Action("GetAlbumList")",
				dataType: "json"
			}),
			dynamic: true,
			reader: {
				read: function (datasource) {
					var count = datasource.data.TotalRowCount;
					datasource.data = datasource.data.Items;
					datasource.data.totalRows = count;
					dataReader.read(datasource);
					}
			}
		});

		$("#remoteTable").wijgrid({
			pageSize: 15,
			data: dataSource,
			allowPaging: true,
			allowSorting: true
		});
	});

At the top, we’re going to declare a new datareader for the JSON coming from the server.  This tells Wijmo how to map JSON properties to Columns in the grid. 

Next is the datasource.  This is a proxy for a URL.  Wijmo will do a GET on this URL to pull information.  The dynamic keyword tells Wijmo that the server will accept sorting, filtering, and paging requests.  If this is false or not set, Wijmo will not send the request parameters we mentioned earlier.  The reader tells WIjmo how to interpret the data coming in, and decipher row counts and then finally apply the datareader.

The last call is the actual WijGrid creation.  This shouldn’t be too different from what you’ve seen in the past.  The only thing that makes Wijmo use the server to gather data is the datasource. 

I hope this serves as an introduction to doing server side calls with Wijmo.  If you have any questions at all, feel free to post them to the comments or email me!

Kevin Griffin
keving@componentone.com

Follow Me On Twitter

Wijmo Wednesday: Measure up with Linear Gauges

Wijmo v2 is here!  Yay!   For today’s Wijmo Wednesday, let’s take a look at a new Wijmo v2 feature: Linear Gauges.

image

Getting Started

To get started, we need to create a new DIV for the gauge to live in.

 

<div id="gauge"></div>

There you go!  Of course, that doesn’t do anything, so let’s write a little bit of JavaScript to turn that DIV into a Linear Gauge.

$("#gauge").wijlineargauge();

If you render this as is, the gauge will look similar to this picture:

image

A couple of things to point out on this gauge.  First is the tick marks.  These are called Major Ticks.  By default, these are set to automatically show for an interval of 10.  There are also Minor Ticks that can be shown in between each major tick.  By default, these are not displayed.  We’ll show you how to change the value in a moment.

Changing Minimum and Maximum Values

There are two properties for setting minimum and maximum values of the linear gauge.  These are called min and max.  Let’s play with the values:

$("#gauge").wijlineargauge({ min: 0, max: 1000 });

Doing this will product an image like so:

image

Whoa!  What’s wrong!?  Well, the tick major is still set to 10, so the gauge is trying to render a tick for every interval from 0 to 1000.  Let’s change the ticks to a more reasonable value.

Setting Tick Intervals

I’m going to play with two new values: tickMajor and tickMinor

$("#gauge").wijlineargauge({
            min: 0,
            max: 1000,
            tickMajor: {
                interval: 200
            },
            tickMinor: {
                interval: 50,
                visible: true
            }
        });

Refresh the page.  Note the bolded visible: true.  This is important since the tickMajor.visible property is defaulted to false.  Here’s what the updated picture looks like:

image

Doesn’t that look much better!  Next, let’s play around with that pointer and set the gauge to a value other than zero.

Setting Pointer Value

$("#gauge").wijlineargauge({
            min: 0,
            max: 1000,
            tickMajor: {
                interval: 200
            },
            tickMinor: {
                interval: 50,
                visible: true
            },
            value: 475
        });

The code change is highlighted above.  Set the value property to update where the pointer goes.  Pictures don’t do this justice, because the transition is animated.

image

If you’d like to update the value dynamically, all you need to do is use jQueryUI property updating syntax:

$("#gauge").wijlineargauge("option", "value", 755);

Ranges

$("#gauge").wijlineargauge({
            min: 0,
            max: 1000,
            tickMajor: {
                interval: 200
            },
            tickMinor: {
                interval: 50,
                visible: true
            },
            value: 475,
            ranges: [
                {
                    startValue: 800,
                    endValue: 1000,
                    startDistance: 0.95,
                    endDistance: 0.75,
                    startWidth: 0.5,
                    endWidth: 0.5,
                    style: {
                        fill: "90-#3DA1D8-#3A6CAC",
                        opacity: "1",
                        stroke: "none"
                    }
                }
            ]
        });

image

The ranges property is an array. Inside the array is a list of range values that are to be applied to the gauge. In this example, I’m only creating a single range, but you can add more. StartValue and endValue are obvious. This is where the range begins and ends. StartWidth and endWidth determine the width (or depending on the orientation it could considered height).   Let’s assume I change the value of startWidth to 0.1.

image

Pretty cool, huh?  Last option is for startDistance and endDistance.  This changes where on gauge the fill starts to render.  For example, I’ll change startDistance to .095 and endDistance to .75 (width is also set back to .5).  The rendered result will “tilt” the range. 

image

Why would this be useful?  Here’s a great example over in the Wijmo control explorer.  It shows how ranges can provide a cool visual effect for showing scale.

image

And there you have it.  All the basics you need to know in order to get started with Wijmo’s linear gauges.  I hope you enjoy working with them!  If you have any questions, feel free to shoot me an email or ask in the comments.

Kevin Griffin
keving@componentone.com

Follow Me on Twitter