The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,874,814
since: 19 Jan 2005

Html.JqGrid - Cleaning up your jqGrid Code

posted Tue 22 Dec 09

I was recently working on a project where I had 5 or 6 different pages, each of which was going to have a full CRUD-enabled jqGrid complete with form editing, date pickers, fields that show up in the form and not on the grid, etc. The problem is after I got finished doing the first one I realized that there was a truckload of really redundant jqGrid code that I didn't want to have to re-type over and over again. Worse, the code is really fragile and if you misplace a comma, bracket, brace, or paren - the debugging of it will give you migraines.

So this is when I decided to write an HTML helper extension, JqGridExtension. This class exposes a single method called JqGrid that you invoke like this from your view page:

<%= Html.JqGrid<ApplicationUser>(
caption:"Users",
entityNameCaption:"User",
gridWidth:700,
formHeight:500,
formWidth:600,
pagerDivName:"pager",
gridTableName:"userGrid",
gridDataUrl:Url.Action("GridData", "User"),
gridEditUrl:Url.Action("SaveUser", "User"),
gridValidateEditUrl:Url.Action("GridValidateUserEdit", "User"),
gridValidateDeleteUrl:Url.Action("GridValidateUserDelete", "User"))
%>

In this code example, you can see that I'm creating a full CRUD grid with form editing (and paging, sorting, and filtering!). The cool part here is that I haven't littered my page with 100+ lines of incredibly difficult to read parameters to the jqGrid plugin. The HTML extension is just a method on a static class that returns a string, like this:

public static class JqGridExtensions
{
public static string JqGrid<T>(this HtmlHelper helper,
string caption,
string entityNameCaption,
int gridWidth,
int formHeight,
int formWidth,
string pagerDivName,
string gridTableName,
string gridDataUrl,
string gridEditUrl,
string gridValidateEditUrl,
string gridValidateDeleteUrl) { .... }

The code then just uses these parameters to spit out all of the tedious jqGrid code that I don't want to have to re-write all the time. There's another little piece of trickery here that I found extremely useful - I'm using a custom attribute that I'm decorating my viewmodel properties with. This custom attribute tells my jqgrid extension how it should handle that column - including whether the column is visible in the grid, visible in the form, editable, what the editor type is (text, datepicker, etc).

All I'm really doing is getting the list of public instance properties on typeof(T). For each of those, I grab my custom attribute (JqGridColModelAttribute) and use the properties on that to build the "colModel" and "columns" jqGrid options and then finally I rig up the navigator and all the other stuff. Because the enterprise's use of grids is standardized to always have a certain set of options, I don't need to even prompt the developer for those - I just implement them the same way throughout the organization. If I need to make organization-wide changes to all of my grids, then I can just tweak my HTML helper method.

Creating an HTML helper extension like this takes very little time to do, but saves the developers on your team a TON of time.

tags:              

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Nick Codignotto left...
Tue 22 Dec 09 5:33 pm :: http://primordia.com/blog

Might also be cool to have the grid dimensions defined by CSS. Does jqgrid facilitate that?


2. Jayashree Wadekar left...
Wed 03 Mar 10 6:13 am

Can you please share the source code for this?


Tag Related Posts

Html.JqGrid - Cleaning up your jqGrid Code

Tue 22 Dec 09 8:46 P GMT-05
tags:              

How to Build your First Azure-Powered MVC App

Tue 29 Sep 09 2:16 P GMT-05
tags:        

ViewState is the Froo-It of the Dev-Il

Wed 23 Sep 09 3:09 P GMT-05
tags:      

Fix for Minor Bug in ASP.NET MVC New Project Template

Mon 04 May 09 2:48 A GMT-05
tags:      

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

Scott Guthrie Updates the ASP.NET MVC Roadmap

Wed 13 Feb 08 3:49 P GMT-05
tags:    

Volta is to Ajax what Tums is to my Stomach

Wed 30 Jan 08 4:11 P GMT-05

ASP.NET 3.5 Extensions Preview Released

Mon 10 Dec 07 2:10 P GMT-05

Microsoft unveils an MVC framework for ASP.NET

Mon 08 Oct 07 12:58 P GMT-05
tags:      

Microsoft Volta - just another codename?

Wed 11 Jul 07 2:43 P GMT-05
tags:        

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

ASP.NET Ajax v1.0 Beta

Fri 27 Oct 06 6:17 P GMT-05
tags:      

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 1

Wed 04 Oct 06 1:37 P GMT-05
tags: