Dojo – Convert Strings to Dates and Sorting them in DataGrid

This sample code demonstrates how to use Dojo to convert strings to date objects. When used in this manner, you can sort the values as “dates” in Dojo DataGrid (otherwise grid handles them as strings)
Note: This code is based on a sample I found on another web-site. I enhanced it for use with dates returned from DQL and added comments to make the code clearer to understand.

WordPress may mess up the formatting: Click the link to get the HTML code as a MS Word file
DateSortingInDojo.docx

@IMPORT url("http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/resources/dojo.css");
@IMPORT url("http://ajax.googleapis.com/ajax/libs/dojo/1.3/dijit/themes/tundra/tundra.css");
@IMPORT url("http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojox/grid/resources/Grid.css");
@IMPORT url("http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojox/grid/resources/tundraGrid.css");

djConfig = {
isDebug: false,
parseOnLoad: true,
baseUrl: "./",
xdWaitSeconds: 10
};

dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.date.locale");

function dateFormatterForDisplay(value, rowIndex) {
//Set the output date format
var aDate = dojo.date.locale.format(value, {datePattern:"dd MMM yyyy HH:mm:ss", selector:"date"});
console.log("formatMyDate():" + aDate);
return aDate;
}

var typeMap = {
"Date": {
type: Date,
deserialize: function(value){
//Parse the String into a date object. Your incoming data-string can be formatted differently from your output.
var aNewDate = dojo.date.locale.parse(value, {datePattern: "dd/MM/yyyy", timePattern: "HH:mm:ss"});
console.log("typeMap called:" + value + "|| converted: " + aNewDate);
return aNewDate;
}
}
};

Part Number Date

Advertisement

One Response to Dojo – Convert Strings to Dates and Sorting them in DataGrid

  1. Michael R says:

    The actual datastore initialization and data structure would’ve been helpful to see on this page as well (it’s viewable in the attached doc), but I found this demo very helpful, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.