If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Grails, jQuery & the jQuery Grid - Part One

I've been intending for quite some time to write a series of articles on my experiences so far with Grails based web applications. So I'm tackling this now whilst my experiences are still relatively fresh before my focus takes another direction.

I don't want to go into the why's of using grails when there are already many other frameworks out there but for me, I love Java for its power, vast number of libraries and community support available. I also love Ruby On Rails, for its convention over configuration benefits so when I discovered Grails is a kind of merge of the two, it quickly gained my interest. There are many cool features to Grails but to pinpoint a couple, not having to go through all the initial setting up of Spring, Hibernate, JPA etc is a real time saver, and how it allows you to use it's own Groovy code alongside pure Java is something in my opinion both very clever and incredible beneficial to us web developers.

My target for these tutorials are not just for beginners to Grails but for anyone looking for quick how-to instructions to use Grails with various Ajax features such as displaying data in a jQuery grid, ajax filtering of data.

I do make the assumption however that you already know the basics of a grails based application and can already create, modify and deploy projects. I also don't want to insist on any particular IDE for working on these tutorials, although I will be using IntelliJ's IDEA IDE, Netbeans also has great Grails support but at the time of writing does not offer gsp tag completion. I cannot offer any other comment on the others as I am not familiar with Grails support in the other main contenders.

I also cannot recommend enough the combination of using Firefox as your browser with the Firebug plugin installed so you can keep an eye of your AJAX calls from your page to server. Firebug is a great tool that allows you to easily see what you are sending and what you are receiving.

In this tutorial I aim to show you how to use the jQuery Grid to present data to your users.

I am using Grails version 1.2 alongside MySQL database.

I will setup a mock up of a customer database for this example, so first you will need to create a new Grails project and setup your datasource details so you are all setup ready to go.

The first step after you've created your project and defined your datasource is to create a new Grails Domain class called Customer. I've kept it simple, but with enough fields for us to create and display several columns in a grid.

Once your class is defined, you should go ahead and generate the customer controller and corresponding views.

Now would be a good time to add some test data so we have something we can use to manipulate our views. A handy shortcut to create test data here is to edit the bootstrap groovy file so we can create some data at the time our application is launched. This is a technique I often use to preload testdata.

jQuery UI css themes : theme download - this is a great site that allows you to visual different UI themes for jquery widgets/plugins before downloading the necessary css/images.

I download the 'min' versions of the javascript librarys to cut down download time for the user. jquery.jqGrid.min.js and jquery-1.3.2.min.js both go into the web-app/js folder.
You theme will be downloaded as a zip file, you will need to copy the jquery-ui-1.7.2.custom.min.js script into your applications js folder and the contents of the css folder go into your web-app/css folder. For the purpose of this tutorial I downloaded the ui-lightness theme.

So now I want to switch from the default list view, to a jQuery Grid to present our customer data.

Edit your Customer/list.gsp, and perform the following.

1. Include our new javascript librarys by adding the includes into the head section. Our css style sheet should also be reference by also adding to the head section. Your head section should then contain the following:

2. Remove the tag and contents of
tag as well as the paginator tag that immediately follows it.3. I usually create page fragments for my jquery specific grids but for this example we will just place the code directly into the main list page.

You have to create a table element to hold our grid, as well as div element to go at the end of the table to display record count, paginator.

Insert the following jquery Grid definition. This defines a very basic grid which will be rendered when the page loads in the browser. Notice the url parameter, url:'jq_customer_list'. This is the url from which the grid will load its data. I have also specified the format of the data will be JSON. These parameters lead us to our next stage, we need to create the necessary function in our Customer controller that will return the data.

Newbie Question - generation of these views?

"Once your class is defined, you should go ahead and generate the customer controller and corresponding views."

Sorry to bother you with such a basic question, but if you aren't using scaffolding in this example, did you roll your own "corresponding views" or is there something else in grails that enables you to auto-generate those?