Saturday, May 01, 2010

Summary on Transaction Rollback in Grails

This post is a summary on how to rollback transactions in grails application, and highlights a stain in grails reference document.

Rollback Programmatic Transactions

In Grails, we can use withTransaction method on domain classes to deal with programmatic transactions. This method takes a closure, which receives a TransactionStatus instance as its first argument. In order to rollback transactions, we can invoke the setRollbackOnly() method on TransactionStatus. Here is an example from grails reference document:
Account.withTransaction { status ->
def source = Account.get(params.from)
def dest = Account.get(params.to)
def amount = params.amount.toInteger()

if(source.active) {
source.balance -= amount
if(dest.active) {
dest.amount += amount
}else {
status.setRollbackOnly()
}
}
}

Rollback Declarative Transactions

With service, it's not a big deal to implement declarative transactions in grails. If you want to rollback, all that you need to do is to throw a RuntimeException or its subclass in service method. The following code shows how to do this job:
class DomainbService {
boolean transactional = true

def service2() {
new B().save()
throw new RuntimeException("save b failed!")
}
}
There is one thing you should notice: grails refernce document doesn't spot the exception you throw in method must be an RuntimeException object. Actually, if you throw an Exception object, the transaction doesn't rollback. You can have a test to verify this. And in spring reference document, you can find that you should throw an "unchecked" exception to trigger a rollback when you are using declarative transaction.

Sunday, March 28, 2010

A simple PageBus taglib

Today, javascript component is a must-be part in many web applications. As more and more such components are included in the web page, the difficulty of interaction between these components is increasing. There is a way to simplify this problem: Event. With it, what you need to do is to register your callback with the event that will be fired by your interested component, and to wait for the magic happened.

All seems good, but "how to register your callback with the component you care about?" If there is a long distance between those two component, this register is not a easy work. That's why I choose PageBus as the backbone of the interaction between components in my project.

If you are familiar with jms, you can simply think pagebus as a jms implemention in the web page. Here is an intuitive picture from its document:



The biggest benefit you can acquire from PageBus is full decoupling between those two parts of interaction. One component doesn't bother another. The whole interaction process is:
  1. subscriber registers a callback with a well-known topic
  2. publisher delivers a message to a well-known topic
  3. callback will be invoked when subscriber gets a message from the topic it registered.
Usage of PageBus is very simple, you just need:
  1. To Download it from here and include pagebus.js in your page.
  2. To register:
        window.PageBus.subscribe(topic, scope, function(sub, message, data){
    .....
    }, data);
  3. To publish:
        window.PageBus.publish(topic, message);
At the end of this post, I will create a simple PageBus taglib to show how to use it.

First, let's create a new Grails App, grails create-app pagebus.

Next, enter your application directory and create a taglib, grails create-tag-lib pagebus.

In your taglib file, write down the code:

class PagebusTagLib {

static namespace= "pagebus"

static final DEFAULT_TOPIC= "app.topic.default"

def include= {
def js= """
<script type="text/javascript">
var App = {
publish: function(topic, message){
window.PageBus.publish(topic, message);
},
subscribe: function(topic, scope, fn, data){
window.PageBus.subscribe(topic, scope, fn, data);
}
}
</script>
"""
out << g.javascript(src:'pagebus/pagebus.js')<< js
}
}
copy pagebus.js to the pagebus/web-app/js/pagebus.

In the app index.gsp,

  • add <pagebus:include/> to <head> section;
  • add a button in your <body> for publishing a "hello world!" message
        <input type="button" value="publish"
    onclick="App.publish('topic1', {message: 'Hello World!'})">
  • register your callback to show the message the subscriber received.
        <g:javascript>
    App.subscribe("topic1", null, function(sub, message, data){
    alert(message.message);
    }, null);
    </g:javascript>
Finally, run your app and test it. Now, you just create your first PageBus application!

Thursday, February 25, 2010

Event Applied: When to minify js and css files in your grails projects

High Performance Web Sites is a great book discussing how to accelerate your web sites. The rule 10 in this book is "Minify JavaScript", which I think can be applied in my grails projects easily.
There are some tools can be used to minify your js and css files, such as JSMIN, Dojo compressor, Edwards' Packer and YUI Compressor. Since I was using YUI these days, I decide to choose YUI Compressor to do this job.
The first solution comes into my mind is to create a Grails Script to process js and css files by "grails create-script". On second thought, I think this is not an appropriate one:
  • If it changes the name of js or css files, you have to change the corresponding files name in your frontend source code. This is annoying and error-prone.
  • If it doesn't change the files' name, it will modify the content of js and css source files. This can also make you unhappy: facing a minified js and css files, you can even hardly change the name of a variable. It is a big problem!
So the solution what I need is: a) which can minify js and css files, b) which can not change the name of files and the content of source code. By controlling the grails application build process, I can achieve my goal. Now, I can hear what you said: Event. That's it! We just need to create an event handler to do this job, and the event we should focus on is : CreateWarStart,which will be fired before grails war your grails application.
Since we know how to do it, let's do it:
  1. Downloading YUI Compressor,which usage is simple:"java -jar yuicompressor-x.x.x.jar inputfile -o outputfile".
  2. Unzipping it and copying YUI Compressor binary to lib directory in your grails project.
  3. Creating a "_Event.groovy" in your grails project's script directory:
       eventCreateWarStart = { warLocation, stagingDir ->
    stagingDir.eachFileRecurse{
    if(it.name.endsWith('.js')||it.name.endsWith('.css')){
    def f= it.absolutePath

    println "Minizing ${f}......................"
    ant.java(jar:"${basedir}/lib/yuicompressor-2.4.2.jar",
    fork: true){
    arg(value: "${f}")
    arg(value: "-o")
    arg(value: "${f}")
    }
    println "Minizing ${f}.................done!"
    }
    }
    }
Only 3 steps, you can minify all the js and css files in your grails projects. Typing "grails war" to test it. Of course, it is not perfect: it always does minification and doesn't care if the js or css files have been minified. If there are many minified files in your projects, which could come from the js lib(such as yui/dojo/jquery) you include, it will waste your cpu time. The way to fix it is simple: you can define an excluding directory list, all directories in it will not be processed.
Another interesting thing is: if there are some errors in your javascript source, YUI Compressor won't minify it(this won't break the build process). So you get an additional checking javascript function ;). Hope you enjoy it!

Saturday, January 16, 2010

A Note On Validation Non Domain classes

Validation non domain classes is a very cool grails feature, but when you use it, you should remember one thing: Invoking clearErrors method before re-validation, specially last validate invocation returns false.
A piece of script means a lot of things:
def u= new foxgem.test1.User1(name:'')
assert u.validate() == false
u.name='foxgem'
assert u.validate() == false
u.clearErrors()
assert u.validate() == true
Here, there is a blank constraint on name property of foxgem.test1.User1. But the note is not necessary to Domain Class, so the second assert statement in the script will failed when foxgem.test1.User1 is a domain class.
This is very confusing, I hope this will be imporved in the future of Grails.

Thursday, November 19, 2009

Getting Beans in Grails Console

It's convenient to run groovy snippets in Grails Console. At some time, these snippets might depend on some Service Object in your grails app. In Grails world, you can solve this kind of dependence using DI. Please check out "8.3 Dependency Injection and Services" in Grails reference document for more information. But now we are running groovy script in grails console, how can we get that service object we need? The reference document doesn't give us an answer directly.
Since Grails is based on Spring, we can resort to ApplicationContext. Now the problem is how to get the ApplicationContext instance. After reading "%GRAILS_HOME%/scripts/Console.groovy", you can find a bind-variable named "ctx", which points to the ApplcationContext Object. So, we can use it to get the dependent object used by the script in the console. Here is an example:
def executionService= ctx.getBean('executionService')
def processInstances= executionService.createProcessInstanceQuery().processDefinitionId('StateSequence-1').list()
def executions = processInstances.collect{
def activeNames= it.findActiveActivityNames()
}

Wednesday, November 04, 2009

JSecurity Mini Guide

This post is a summary on what I learned from my project during I was implementing its security function. The tools and their versions are:
  • Jsecurity 0.9
  • Grails 1.1.1
  • Grails JSecurity Plugin 0.4.1
Maybe the versions of the tools you are using are not the same as me, but I think that is not a big question and migration is not too difficult, ;)
First, let's check grails-jsecurity-plugin. As I said in my last post, this plugin is awesome! If you decide to take jescurity as the fundation of your security function, you should also consider to examine this plugin. Trust me, it will sppeed up your development. Now, it's time to see how to use it:
  1. grails install-plugin jsecurity, or you can download it from http://grails.org/plugin/shiro, then grails install-plugin plugin-path
  2. grails quick-start, this is the simplest way to use this plugin. In this step, you will get 6 security domain classes (including JsecPermission, JsecRole, JsecUser, JsecRolePermissionRel, JsecUserPermissionRel and JsecUserRoleRel), a DbRealm(JsecDbRealm), an AuthController and a login page. All of those classes will meet your need at most of time. If you are using ladp, you can try the LadpRealm provided by this plugin, using "grails create-ldap-realm".
  3. grails create-filters filters-name, this filter will inteceptor all the request, if you want to protect something, this is a good place. To protect what you want to keep, you can leaveage the access control dsl made by this plugin:
    • need authentication? accessControll { true }
    • has role? accessControll{ role('admin') }
    • is permitted? accessControll{ permission("${actionName}:${actionName}:${params.id}") }
    At this point, you have implemented an auth controller and a login page, the security domain classes and a request inteceptor, the rest of the work are:
  4. Using the tag lib to enhance your UI.
  5. Configurating WildcardPermission as the permission class of your application. In BootStrap.groovy:
    new JsecPermission(type: 'org.jsecurity.authz.permission.WildcardPermission', possibleActions: '*').save()
    Except WildcardPermission, You also can use AllPermission to represent a permission owned by administrator:
    new JsecPermission(type: 'org.jsecurity.authz.permission.AllPermission', possibleActions: '*').save()
  6. Implementing the CRUD of those security domain classes. Here, I will show you some samples:
    • add user: new JsecUser(username: 'foxgem', passwordHash: new Sha1Hash('foxgem').toHex()).save()
    • add role: new JsecRole(name: 'admin').save()
    • assign a role to a user:
    • def role= JsecRole.get(1)
      def user= JsecUser.get(1)
      new JsecUserRoleRel(role: role, user: user).save()
    • assign a permission to a user:
    • def user= JsecUser.get(1)
      def permission= JsecPermission.get(1)
      // In this sample, the target property is a wildcard permission string, eg: 'controller:action'.
      // Please to check WildcardPermission in JSecurity API document for more information.
      new JsecUserPermissionRel(permission: permission, user: user, target: params.target, actions: '*').save()
Now, you have all the knowledge about grails jsecurity plugin. Next, you should understand the fundation of this plugin - JSecurity. The more you know about it, the more you can do with it. The following diagram shows the key concepts you must know, for more information, please read jsecurity api document.

At the end of this post, I will list some helpful resources:

Tuesday, October 20, 2009

How to get actions of a controller

I'm doing something about security in my project using grails jsecurity plugin. To make this function user-friendly, I want to implement an ui that admin can assign a slected controller or its actions to an user or a role. It's not a big deal to list all controllers within a grails app: you can use "grailsApplication.controllerClasses". So, the next step is how to get actions of a controller.
Google is a good teacher when you meet trouble. But I can google nothing. It seems that there is not any thread about my question. So, I checked grails api and found two things:
1. GrailsControllerClass does not provide a method to list all actions.
2. But GrailsControllerClass has a method to list all its possible URIs: getURIs().
Since an URI is a combination of controller and action in grails, finally, I solved my problem using that method. Here is my solution:

def actions= []
grailsApplication.getArtefact(ControllerArtefactHandler.TYPE, your controller name).URIs.each{
def parts= it.split('/')
if(parts.size()>2){
actions << parts[2]
}
}
return (actions as Set)

By the way, Grails JSecurity Plugin is awesome! It reallys makes my life easier!

Thursday, September 24, 2009

More about Object Marshaller

Should I need to know something about Object Marshaller since Grails has done such good job in this aspect? Most of the time, the answer is not. But if you are doing something about RIA or Ajax, the answer is yes.
Why? The reason is maybe:
1. You want to include some properties in JSON output which was not included in default, such as version. By the way, you can include version through three config option in grails new version. You can find more details on this issue at this or my last post.
2. You like to format some property yourself, such as date formatting
3. You want to output a object into a new JSON string, such as I want to output a relation property's toString() not its id.
4. ...
These days I was doing a RIA application using grails and ext. And, I met all the three requirements I said above. And here is what I got during this project:
1. To implement your marshaller, you can implement ObjectMarshaller or you can use closure when you register it. If you want to use closure, you can do this:

JSON.registerObjectMarshaller(Locale.class){
def rval= [:]
rval['ID']= it.toString()
rval['displayName']= it.getDisplayName()
return rval
}

For method 1, implementing ObjectMarshaller, You can check DomainClassMarshaller.java. But if what you want is just to output the return value of toString() of relation property, you really don't have to do it from the ground up. You can extend DomainClassMarshaller:

class AnotherDomainClassMarshaller extends DomainClassMarshaller{
protected void asShortObject(Object refObj, JSON json,
GrailsDomainClassProperty idProperty,
GrailsDomainClass referencedDomainClass) throws ConverterException {
JSONWriter writer = json.getWriter();
writer.object();
writer.key("class").value(referencedDomainClass.getName());
writer.key("id").value(extractValue(refObj, idProperty));
writer.key("value").value(refObj.toString());
writer.endObject();
}
}

2. After you create your marshaller, you should register it. And the best place in grails application is BootStrap.groovy:
JSON.registerObjectMarshaller(your marshaller instance)

3. If you want to register more than one marshaller, you should provide a priority. REMEMBER IT! Yes, it is a trick. If you ignore it, you will get a surprise: Your marshaller registered just now did not work! After I digged the source of grails, DefaultConverterConfiguration.java, I found the truth: Grails hold all the object marshallers in a treeset, which use priority as a comparator, if you register two marshallers with the same priority, grails will register your marshaller with default-priority(=0) if priority is missing, the second marshaller will not be registered. So my last version of BootStrap.groovy is:

JSON.registerObjectMarshaller(...)
JSON.registerObjectMarshaller(..., 1)
JSON.registerObjectMarshaller(..., 2)
But there is a problem I don't understand: the default ByteArrayMarshaller did not work, and I have to register it myself and provide a priority like this:

JSON.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.json.ByteArrayMarshaller(), 1)
Who can tell me why?

Wednesday, August 26, 2009

Another way to include version property in your JSON

For those want to include domain class version property in JSON, you can get help from GRAILS-4530: Chaning your grails version or Using the way provided by Sigi(See the description).
But I found another way to get that. Instead of using a new DomainClassMarshaller, I just retrieve the DomainClassMarshaller from the ConverterConfiguration. Here is my code:
import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder as CCH
import org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller as DCM
import grails.converters.JSON

class BootStrap {

def init = { servletContext ->

def cfg= CCH.getConverterConfiguration(JSON.class)
def marshellers= cfg.orderedObjectMarshallers
for(m in marshellers){
if(DCM.class.isAssignableFrom(m.class)){
m.includeVersion= true
break
}
}
}
def destroy = {
}
}
Maybe it is not as simple as Sigi's solution, but I think it is the complement to this issue.

Monday, August 24, 2009

A new Grails powered site launched

A new Grails powered site launched last Friday: GroovyLive, which is a simple groovy console with a simple tutorial.
Here are features of this simple Grails App:
  • Hosted on Google's GAE
  • Script history
  • Supporting hotkey. You can find these keys though the tooptips of the buttons of console.
  • A simple Groovy tutorial, although not including Groovy MOP.
  • The tutorial is intractive. You can try the script included in tutorial in a command shell, and the content will be the next chapter when the result of that script matches the answer.
It is powerd by:
Since English is not my native language, if there are some error in my tutorial, pls point it to me.
Thanks.

Friday, July 17, 2009

Using a list or a map to construct JSON dynamiclly

How do you contruct a dynamic JSON, especially when you only need to return some fields of an object not a whole object? Using a builder or constructing a string directly? Neither is my favorite.
As time playing grails goes on, I find another way to build a JSON, only two steps:
1. To fill a list or a map with your data.
2. Using "as JSON".
Here is a sample:

def toc= TutorialRepository.toc
int i=1
int j=1
def tocJson=[]
toc.each{ ch ->
def chJson= [:]
chJson['title']= ch.title
chJson['sections']= []
ch.sections.each{ se ->
def seJson= [:]
seJson['title']= se.title
seJson['pos']= "${i}:${j}"
chJson['sections'] << seJson
j++
}
tocJson << chJson
i++
j= 1
}

render tocJson as JSON

It's simple, right? And there are several advantages in this way building a JSON:
1. It makes code clean.
2. You can build more expressive JSON.
3. It is more intuitive as the structure of the list(or the map) is the structure of the final JSON.

Sunday, July 12, 2009

Enhancing your layout with YUI Layout Manager

These days, I did a little grails project. And in this project, I combined sitemesh with yui layout manager. The way is not too difficult ;)
Here is code snippet from layout gsp, eg main.gsp:

<head>
...
<script type="text/javascript">
YAHOO.util.Event.onDOMReady(function () {
var layout1 = new YAHOO.widget.Layout({
minWidth: 1000,
units: [
{ position: 'top', height: 45, body: 'top1' },
{ position: 'left', width: 190, resize: true, body: 'left1', gutter: '2px', header: leftHeader,
collapse: true, scroll: true },
{ position: 'center' }
]
});
layout1.on("render", function(){
var el = layout1.getUnitByPosition('center').get('wrap');
var layout2= new YAHOO.widget.Layout(el, {
parent: layout1,
units: [
{ position: 'top', height: 45, body: 'top2', gutter: '2px' } ,
{ position: 'center', body: 'center2', gutter: '2px' },
{ position: 'bottom', height: bottomHeight, resize: true, body: 'bottom2',
scroll: true, gutter: '2px' }
]
});
layout2.on("resize", function(){
YAHOO.util.Dom.setStyle(center2ContentId, 'height', ((layout2.getSizes().center.h - 7) + 'px'));
});
layout2.render();
});
layout1.render();
init();
});
</script>
</head>
<body class="${pageProperty(name:'body.class')}">
<div id="top1">
<div id="logo"></div>
</div>
<div id="left1">
<g:pageProperty name="page.layout1.left.content"/>
</div>
<div id="top2">
<div id="toolbar"></div>
</div>
<div id="center2">
<g:pageProperty name="page.layout2.center.content"/>
</div>
<div id="bottom2">
<g:pageProperty name="page.layout2.bottom.content"/>
</div>
<g:pageProperty name="page.other"/>
</body>

init() and <g:pageProperty> is just placeholder, you can override it in your any gsp decorated by this layout. For example:

<head>
<script type="text/javascript">
// your own js code is here
...
// define your init function
var init= function(){...};
</script>
</head>
<!-- define your page content -->
<body class="yui-skin-sam">
<content tag="layout1.left.content"></content>
<content tag="layout2.center.content">
<div id="..."></div>
</content>
<content tag="layout2.bottom.content">
<div id="...">
<div class="..."></div>
</div>
</content>
<content tag="other">
<div id="how-to" style="text-align:left;">
......
</div>
</content>
</body>

It is noted that YUI Layout manager in this sample can also be replaced with othere ajax components lib(eg extjs or dojo) without too much hard work.

Monday, April 27, 2009

How to change result of GrailsUI DataTable in an ajax way

DataTable is a good way to show data, but if you can give a way to your application users to change its result at rumtime they will get a better experience. GrailsUI DataTable taglib has an attribute named "params", which value is a query string and will be sent to your data reader since the first time DataTable sends request. So the thing becomes simple, if you can:

  1. change this attribute value at runtime

  2. change your data reader to check this query string, filter data by it and return result as JSON that DataTable needs.


Then you can change the content of DataTable as your users' wish.
There are two implement styles - one is a server-side way; the other is an ajax way, which will be this blog's topic. Since the server-side way is not complicated, I think you can do it yourself. ;)
The Domain class I will use in this example is following, and the fields used for filter are title and closed.
class Question {
String title
String details
Boolean closed
Date dateCreated
Date lastUpdated
}

First, let's see the simple part of the question: How to filter data and return the result. It's really simple, and any grails developer will finish it in no time:
def load = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
def fields=[]
def fieldsCount= 0

def questionList=[]
if(!params.title && !params.closed){
fieldsCount= Question.count()
questionList= Question.list(params)
}else{
def c= Question.createCriteria()
fieldsCount= c.get{
projections{
count('id')
}
if(!params.closed params.closed=='all'){
like("title", "%${params.title?:''}%")
}else{
def closed= params.closed=='y'? true:false
and{
like("title", "%${params.title?:''}%")
eq("closed", closed)
}
}
}
questionList= Question.withCriteria{
if(!params.closed params.closed=='all'){
like("title", "%${params.title?:''}%")
}else{
def closed= params.closed=='y'? true:false
and{
like("title", "%${params.title?:''}%")
eq("closed", closed)
}
}
maxResults(params.max)
firstResult(params.offset? params.offset.toInteger():0)
}
}

questionList.each{ question ->
fields << [
id: question.id
,title: question.title
,dateCreated: g.formatDate(date:question.dateCreated, format:'yyyy-MM-hh HH:mm')
,lastUpdated: g.formatDate(date:question.lastUpdated, format:'yyyy-MM-hh HH:mm')
,closed: question.closed
,action: """<A href='/codeline/question/edit/${question.id}'>update</A>
${ if(!question.closed){
"<br/><A href='/codeline/question/delete/${question.id}'>delete</A>"
}else{
""
}
}
"""
]
}

response.setHeader("Cache-Control", "no-store")
def result= [
totalRecords: fieldsCount
, results: fields]
render result as JSON
}

Then I will show you the markup of page: a query zone and a datatable:
<div class="roundbg roundspace">
<div class="roundtl"><div class="roundbr"><div class="roundtrc"><div class="roundblc roundpad">
<h5 class="title_ico">Question List</h5>
<div class="list_toolbar"><form>
<label for="title">Title:</label>
<input type="text" maxlength="30" id="title" name="title"/>
<label for="closed">Closed?:</label>
<input type="radio" id="closed" name="closed" value="all" class="cssform_radiobutton" checked/>All
<input type="radio" id="closed" name="closed" value="y" class="cssform_radiobutton"/>Y
<input type="radio" id="closed" name="closed" value="n" class="cssform_radiobutton"/>N
<input type="button" value="submit" class="button" onclick="query">
</form></div>

<gui:dataTable id="questions" draggableColumns="true"
columnDefs="[
[key:'id', sortable:true, resizeable: true, label:'ID']
,[key:'title', sortable:true, resizeable: true, label:'Title']
,[key:'dateCreated', sortable:true, resizeable:true, label:'Created Time']
,[key:'lastUpdated', sortable:true, resizeable:true, label:'Updated Time']
,[key:'closed', label:'Is closed?']
,[key:'action', label:'Action']
]"
controller="question" action="load" rowsPerPage="1"/>
</div></div></div></div>

You may notice the submit button has a handler named "query", which will build a query string and send it to the action given above. Its code is here:
<script type="text/javascript">
var query = function(){
var str= '';
if(YAHOO.util.Dom.get('title').value.length>0){
str+= "title=" + YAHOO.util.Dom.get('title').value;
}

if(str.length>0){
str+= '&'
}
str+= "closed=" + YAHOO.util.Selector.query('#closed:checked')[0].value;
GRAILSUI.questions.customQueryString = str;

GRAILSUI.questions.loadingDialog.show();
GRAILSUI.questions.cleanup();
var sortedBy = GRAILSUI.questions.get('sortedBy');
var newState = {
startIndex: 0,
sorting: {
key: sortedBy.key,
dir: ((sortedBy.dir === YAHOO.widget.DataTable.CLASS_DESC) ? YAHOO.widget.DataTable.CLASS_DESC : YAHOO.widget.DataTable.CLASS_ASC)
},
pagination : {
recordOffset: 0,
rowsPerPage: GRAILSUI.questions.get("paginator").getRowsPerPage()
}
};
var oCallback = {
success: GRAILSUI.questions.onDataReturnInitializeTable,
failure: GRAILSUI.questions.onDataReturnInitializeTable,
scope: GRAILSUI.questions,
argument: newState
};
GRAILSUI.questions.getDataSource().sendRequest(GRAILSUI.questions.buildQueryString(newState), oCallback);
}
</script>

This code needs to be explained carefully:

  • In the example I did not use GrailsUI DataTable taglib's attribute "params", but used "customQueryString" instead. The reason is that the value of "params" will be passed to this attribute. Its purpose is to give you a way to define your own query string, its value will be a part of url that the DataSource owned by DataTable request. And this url will be built by buildQueryString()

  • The remains of query fucntion was mainly copied from GRAILSUI.DataTable.onPaginatorChangeRequest, which is in "%grailsui-home%/web-app/js/grailsui/DataTable.js". And there are two differences:


    • startIndex and recordOffser is zero

    • success handler and failure handler is not onDataReturnSetRows but onDataReturnInitializeTable

Hope you enjoy it.

Wednesday, April 15, 2009

A simple way to customize format for GrailsUI DataTable Cells

I was playing around with grails-ui these days.I really like this plugin, it makes my life easy.

After somedays digging, I find a simple way to custom formatting for DataTable cells. It is so simple that I think you will understand it quickly after reading the following controller action code. There is nothing different in GSP, so its code is omited.
def load = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
def fields=[]
Question.list(params).each{
fields << [
id: it.id
,title: it.title
,dateCreated: it.dateCreated
,lastUpdated: it.lastUpdated
,closed: it.closed
,updateAction: "<A href='/codeline/question/edit/${it.id}'>update</A>"
,deleteAction: "<A href='/codeline/question/delete/${it.id}'>delete</A>"
]
}
response.setHeader("Cache-Control", "no-store")
def result= [ totalRecords: Question.count(), results: fields]
render result as JSON
}
Do you follow me? That's right! It is "returning html in the result". Be careful, if the html is complex, the best way is to check the YUI Document.