<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jamesg's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 00:23:14 GMT</pubDate>
    <description>DZone Snippets: Jamesg's Code Snippets</description>
    <item>
      <title>YUI DataTable - Understanding ISO / SQL Date Time Formats</title>
      <link>http://snippets.dzone.com/posts/show/5338</link>
      <description>As of &lt;a href="http://developer.yahoo.com/yui/"&gt;YUI&lt;/a&gt; 2.5.1 the DataSource utility (which feeds the DataTable widget) only accepts either native Javascript Date objects or a string formatted to the North American locale (mm/dd/yyyyy) when using parser: "date" in your schema.&lt;br /&gt;&lt;br /&gt;This can cause problems outside the USA, particularly if you are using ISO date formats say from MySQL DateTime columns. So let's fix this and include a Calendar widget for editing.&lt;br /&gt;&lt;br /&gt;First, we depend on &lt;a href="http://www.datejs.com/"&gt;Datejs&lt;/a&gt; so ensure you've already got this loaded into the application. Next you need a couple of custom callback methods&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * Accepts a date string in any format that datejs.com's library can recognise.&lt;br /&gt; * Returns a Date object if successful, false otherwise&lt;br /&gt; */&lt;br /&gt;parseSQLDate = function(data)&lt;br /&gt;{&lt;br /&gt;       var date = null;&lt;br /&gt;       try {date = Date.parse(data);return date;}&lt;br /&gt;       catch(e) {return false;}&lt;br /&gt;};&lt;br /&gt;/**&lt;br /&gt; * This is a formatting callback for YUI DataTable to present date in ISO format&lt;br /&gt; * instead of American mm/dd/yyyy&lt;br /&gt; */&lt;br /&gt;formatDateInDataTable = function(elCell, oRecord, oColumn, oData)&lt;br /&gt;{&lt;br /&gt;       var oDate = oData;&lt;br /&gt;       elCell.innerHTML = oDate.toString('yyyy-MM-dd');&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now to activate modify your schema thus:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dataSource.responseSchema = {&lt;br /&gt;       resultsList: "items",&lt;br /&gt;       fields: [{key: "created_on", parser: parseSQLDate}]&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And in your columnDefs:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dataSource.columnDefs = [&lt;br /&gt;       {key: "created_on", label: "Created", editor: "date", formatter: formatDateInDataTable}&lt;br /&gt;];&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Lastly we set up an editor for cells:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dataTable.subscribe("cellClickEvent", dataTable.onEventShowCellEditor);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 08 Apr 2008 22:16:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5338</guid>
      <author>jamesg (James Green)</author>
    </item>
  </channel>
</rss>
