Adding a date field to a dgrid

678
1
Jump to solution
11-07-2013 10:39 AM
BrettGreenfield__DNR_
Occasional Contributor II
I'm using the following sample: https://developers.arcgis.com/en/javascript/jssamples/fl_dgrid.html

One of my fields is a date field, and I'm having trouble getting it to show up properly (they're showing up as a string of numbers).

I've been going through the documentation but haven't had much luck figuring out how to tell dojo to read it as a date and not a string.
0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor
In order to display it as a date string (instead of the Epoch numbers you are currently seeing), you will need to add a formatter function to your data grid for your date field. On the HTML side, add the formatter option on the appropriate line of the table headers like this:
[HTML] <table dojotype="dojox.grid.DataGrid" jsid="grid" id="grid" selectionMode="none">
<thead>
<tr>
<th field="OBJECTID" width="0%">OBJECTID</th>
<th field="rdName" width="100%">rdName</th>
<th field="dateCl" formatter="formatGridDate" width="0%">dateCL</th>
<th field="dateOp" width="0%">dateOp</th>
</tr>
</thead>
</table>[/HTML]

Next, in JS, you create your formatting function something like this:
function formatGridDate(theDate, rowIndex) {      var rowdata = this.grid.getItem(rowIndex);      var theDate = new Date(parseInt(rowdata.<your date filed name here>));       theDateString =  dojo.date.locale.format(theDate, {    selector: 'date',    datePattern: 'MM/dd/yyyy'   });       return theDateString; }


You can refer to the Dojo documentation about this here.

Good luck!
Steve

View solution in original post

0 Kudos
1 Reply
SteveCole
Frequent Contributor
In order to display it as a date string (instead of the Epoch numbers you are currently seeing), you will need to add a formatter function to your data grid for your date field. On the HTML side, add the formatter option on the appropriate line of the table headers like this:
[HTML] <table dojotype="dojox.grid.DataGrid" jsid="grid" id="grid" selectionMode="none">
<thead>
<tr>
<th field="OBJECTID" width="0%">OBJECTID</th>
<th field="rdName" width="100%">rdName</th>
<th field="dateCl" formatter="formatGridDate" width="0%">dateCL</th>
<th field="dateOp" width="0%">dateOp</th>
</tr>
</thead>
</table>[/HTML]

Next, in JS, you create your formatting function something like this:
function formatGridDate(theDate, rowIndex) {      var rowdata = this.grid.getItem(rowIndex);      var theDate = new Date(parseInt(rowdata.<your date filed name here>));       theDateString =  dojo.date.locale.format(theDate, {    selector: 'date',    datePattern: 'MM/dd/yyyy'   });       return theDateString; }


You can refer to the Dojo documentation about this here.

Good luck!
Steve
0 Kudos