30
2010
Funky Grails Tables with YUI DataTable
Need to present some tablular data sources from a backend datasource and looking for a slick and painless cross-browser solution? You should definitely take a look at the YUI Datatable.
I’ve been using it on one of our internal projects where we need to present a large amount of tabular data, but let the client sort it as they need to. Our first draft has given us something like this:
One of the things I love about YUI is that the documentation and examples are first class, and with the new Dependency Configurator, getting your CSS and JS organised is much more straightforward.
You can use YUI controls with backend JSON, XML or even in-page HTML elements, but for my purposes, I needed to dynamically change the table based an incoming specification id. My backend controller does a little magic on how it slices the data, but you might consider a simpler version as something like:
def listData = {
def lib= ControlLibrary.get(params.id)
def controlsJSON = [ controls : lib.controls]
render controlsJSON as JSON
}
So we’re just rending some JSON which consists of a key of “controls” followed by a list of individual controls (which is the data you see in the cell). Now, on to the client side. First go and sort out your dependencies, then we’ll get cracking with some Javascript…
You’ll need a div for the control to land in. So let’s spec that out first:
Ok. We have our div in place, it’s time to setup the Datatable code itself. YUI components typically rely on a DataSource component to do the actual fetching of the remote data. You tell the DataSource what kind of data to expect (JSON, XML, etc), give it a way to map the incoming data into something useful to you, and then feed it to a UI component for rendering. Let’s take that one step at a time… First let’s setup the DataSource… we’ll wrap it in a window “load” event to make sure it doesn’t fire until the page is loaded and all our JS and CSS is pulled down…
YAHOO.util.Event.addListener(window, "load", function() {
// Setup your backend URL for retrieving the data
var myDataSource = new YAHOO.util.XHRDataSource(' ');
myDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
// The response schema let's you map out the JSON fields you want to expose
myDataSource.responseSchema = {
resultsList : "controls",
fields : [
{ key : "id" },
{ key : "reference"},
{ key : "name"},
{ key : "category" },
{ key : "compliance" },
{ key : "classification"}
]
}
I’m returning the JSON to consist of a “controls” element, so I’ll tell YUI to look in that JSON element for the data. Under that, my fields consist of id, reference, name, category, etc. I don’t need all the fields, so I just map the ones I need.
So I now have the data coming back, so it’s time to setup the table proper:
// map our JSON fields to column labels
var myColumnDefs = [
{ key: "reference", label: "Spec", sortable: true },
{ key: "name", label: "Name", sortable: true },
{ key: "category", label: "Category", sortable: true },
{ key: "compliance", label: "Compliance", sortable: true },
{ key: "classification", label: "Classification", sortable: true }
];
// Now setup the table itself, giving datasource, and hint at presorted data
var myDataTable = new YAHOO.widget.DataTable("myContainer", myColumnDefs, myDataSource, {
sortedBy : {
key: "reference",
dir: YAHOO.widget.DataTable.CLASS_ASC
}
});
So the final step is to map our JSON fields through to proper table column names. In the example about the “reference” field appears titled as “Spec” in the table. Finally, we instantiate the table, and tell is that the data is pre-sorted by the “reference” field, so that the control can automatically add the relevent “tick” mark to show the sort direction.
There’s a bazillion other features you can using in the Datatable (including pagination, polling of the datasource, context menus and lots more). Go check out the examples for more info.
I’ve had tons of great experiences working with YUI components, and best of all, everything seems to work fine in Internet Explorer! Can’t wait to sink my teeth into YUI3.
2 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





Hi,
Good intro to the possibilities b/w Grails & YUI Question – what is the tag doing at the bottom of the listener method. May be I am missing something.
Thanks Kris. Unbelieveable…. that tag is added by the Javascript that does the syntax colouring. Need to workout how to switch off the “autoclose” tag feature