This is the 3rd blog post in a series taken from a recent ASP.NET MVC & jQuery presentation I gave at CodeStock. The previous blog posts are available here...
MVC Contrib Grid Component
The ASP.NET MVC 1.0 Release has a lot of great things included in it, but there is another open source project called MVC Contrib that fills many gaps and compliments the core functionality.
In this blog post I want to demonstrate how you can use the MVC Contrib Grid Component in conjunction with two different jQuery plugins to provide rich functionality inside a simple and clean View.
First lets review why you might want to use the MVC Contrib Grid Component.
- Provides a Fluent Interface
- Discourages Logic in your View
- Provides Reusable Grid Models
- Customizes Grid Rendering
- Includes a Separate Pagination Component
If the above bullet items interest you, then check out the 5 part blog series on the MVC Contrib Grid Html Helper by Jeremy Skinner.
Enough of describing the component, lets start using the MVC Contrib Grid. After adding a reference to the MvcContrib assembly to your project you can proceed adding the Grid to your View…
<% Html.Grid(Model).Columns( column => { column.For(x => Html.ActionLink("ID", "Details", new { id = x.Id })).Named("View").DoNotEncode(); column.For(x => x.Name).Named("Pet Name"); column.For(x => x.Description).Named("Description"); column.For(x => x.Price).Format("{0:C}"); }) .Attributes(style => "width: 75%;") .Empty("There are no pets.") .Render(); %>
As you can see in the code above, the syntax is easy to ready and it utilizes the tightly bound model passed to the View. The output of the Grid isn’t anything spectacular (see the following grid).
<table id="dataTable"> <thead> <tr> <th>View</th> <th>Pet Name</th> <th>Description</th> <th>Price</th> </tr> </thead> <tr class="gridrow"> <td><a href="/MvcContrib/Details/1">ID</a></td> <td>Ralph Fits</td> <td>A lonely ragged pup that licks himself and isn't house trained</td> <td>$12.99</td> </tr> <tr class="gridrow_alternate"> <td><a href="/MvcContrib/Details/2">ID</a></td> <td>Fluffy Florentine</td> <td>A sweet little kitten that is ready to have a home</td> <td>$277.00</td> </tr> <!-- Additional Rows --> <tr class="gridrow_alternate"> <td><a href="/MvcContrib/Details/14">ID</a></td> <td>Alien Abbott</td> <td>You never hear this dog bark because it only barks in the future.</td> <td>$777.99</td> </tr> <tr class="gridrow"> <td><a href="/MvcContrib/Details/15">ID</a></td> <td>Crazy Calico</td> <td>If you like cats, don't get this one because it's pure crazy on a stick!</td> <td>$2.99</td> </tr> </tab
We can now transform the output of the MVC Contrib Grid into a rich featured table with the help of a little jQuery.
jQuery TableSorter Plugin
First, lets use the jQuery TableSorter Plugin to add some simple inline sorting capabilities to the Grid. Only the following one line of jQuery is necessary to transform this Grid. See the follow table for the results.
Note: TableSorter has additional widgets you can add for extra functionality. I am using the zebra widget to provide the alternate row coloring.
$("table").addClass('tableSorter').tablesorter({ sortList: [[1, 0]], widgets: ['zebra'] });
View | Pet Name | Description | Price |
---|---|---|---|
ID | Ralph Fits | A lonely ragged pup that licks himself and isn't house trained | $12.99 |
ID | Fluffy Florentine | A sweet little kitten that is ready to have a home | $277.00 |
ID | Bubba Gump | A very stupid dog that can't even find his way to his dog house | $999.99 |
ID | Charlie Strango | This weird looking puppy scares most adults, but has a strange interest to children | $23.44 |
ID | Blacky the Beautiful | This beautiful cat is as kind as can be. Who says that a dog is man's best friend? | $45.65 |
ID | Snot Nose Nelly | If you want to get wet, then this is the pet for you. Talk about snot everywhere! | $44.78 |
ID | Darcy Scoop | A wonderful black lab with human-like intelligence; however, it does poop a lot. | $234.55 |
ID | Supsrirooantsum | A very rare cat straight from it's homeland of India. | $432.67 |
ID | Clark Kent | A self-employed kitten making and selling fur balls for all to enjoy. | $44.66 |
ID | Red Headed Bull | This dog is unlike any other that you have seen. You can even braid it's flowing red hair. | $78.99 |
ID | Rex the Terrible | A half german shepard / half wolf aggresive dog that will tear the flesh out of anyone you wish. | $346.99 |
ID | Skittish Steve | You will never see this dog once you buy it. It'll be like you never had a pet to begin with. | $33.00 |
ID | Dart Mouth | A dart gun was surgically grafted into this dog's mouth. He is great for parties. | $55.00 |
ID | Alien Abbott | You never hear this dog bark because it only barks in the future. | $777.99 |
ID | Crazy Calico | If you like cats, don't get this one because it's pure crazy on a stick! | $2.99 |
jQuery DataTable Plugin
Now, lets try adding a s lightly more feature rich jQuery Plugin to our Grid. The jQuery DataTable Plugin allows not only sorting of columns, but provides an elegant searching mechanism and also paging functionality. Yet again, with only one jQuery statement we can transform this table into something much more usable.
$('dataTable').addClass('dataTable').dataTable({ "iDisplayLength": 10, "aaSorting": [[1, "asc"]], "aoColumns": [{ "bSortable": false }, null, null, null] });
View | Pet Name | Description | Price |
---|---|---|---|
ID | Ralph Fits | A lonely ragged pup that licks himself and isn't house trained | $12.99 |
ID | Fluffy Florentine | A sweet little kitten that is ready to have a home | $277.00 |
ID | Bubba Gump | A very stupid dog that can't even find his way to his dog house | $999.99 |
ID | Charlie Strango | This weird looking puppy scares most adults, but has a strange interest to children | $23.44 |
ID | Blacky the Beautiful | This beautiful cat is as kind as can be. Who says that a dog is man's best friend? | $45.65 |
ID | Snot Nose Nelly | If you want to get wet, then this is the pet for you. Talk about snot everywhere! | $44.78 |
ID | Darcy Scoop | A wonderful black lab with human-like intelligence; however, it does poop a lot. | $234.55 |
ID | Supsrirooantsum | A very rare cat straight from it's homeland of India. | $432.67 |
ID | Clark Kent | A self-employed kitten making and selling fur balls for all to enjoy. | $44.66 |
ID | Red Headed Bull | This dog is unlike any other that you have seen. You can even braid it's flowing red hair. | $78.99 |
ID | Rex the Terrible | A half german shepard / half wolf aggresive dog that will tear the flesh out of anyone you wish. | $346.99 |
ID | Skittish Steve | You will never see this dog once you buy it. It'll be like you never had a pet to begin with. | $33.00 |
ID | Dart Mouth | A dart gun was surgically grafted into this dog's mouth. He is great for parties. | $55.00 |
ID | Alien Abbott | You never hear this dog bark because it only barks in the future. | $777.99 |
ID | Crazy Calico | If you like cats, don't get this one because it's pure crazy on a stick! | $2.99 |
Now, there are definitely some pros and cons in using the above two jQuery plugins.
Pros
- The TableSorter Plugin & DataTable Plugin are very easy to implement. Only one jQuery statement is needed.
- The user is presented with a richer interface providing sorting, paging, and simple searching.
Cons
- Despite the pros listed above, the biggest con is that the jQuery Plugins are not scalable. All the records are returned in the View and the plugins hide and show the necessary data depending upon the user’s actions.
In light of the cons, I personally wouldn’t use any of the above jQuery plugins for my tables unless it was for a trivial non-production web page with a known number of static records.
Normally, I lead towards using either the Flexigrid Plugin or jqGrid Plugin that I will be reviewing in the next blog post in this series.
No comments:
Post a Comment