我有一个带有日期列的Dojo Grid。尽管使用了自定义格式化器,但它总是执行字符串排序。请注意,我的接收日期将被格式化为02/17/2005,我不能更改它。
formatDate = function(d){
return dojo.date.locale.format(new Date(d), {datePattern: "M/dd/yyyy", formatLength: 'short', selector: "date"});
}
var myData= [
{field: "transactionDate", name: "Transaction Date", width: "115px", datatype: "date", formatter: formatDate },
{field: "description", name: "Transaction Description", width: "170px" },
]];
var storeData = {
items: [
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"02/15/2010",
description:"A description of the transaction"
}
,
{
transactionDate:"01/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"09/15/2009",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
,
{
transactionDate:"04/15/2011",
description:"A description of the transaction"
}
]};
<div class="claro" style="width: 835px; height: 300px;">
<div dojotype="dojo.data.ItemFileReadStore" jsID="dataStoreForGrid" data="storeData"></div>
<div id="grid" dojotype="dojox.grid.DataGrid" store="transferStoreForGrid" clientSort="true" jsID="SomeId" structure="myData" rowsperpage="40" ></div>
</div>
根据Peller的建议,我尝试格式化为ISO日期。泰语对排序没有影响:
formatDate = function(d){
var d2 = dojo.date.stamp.fromISOString(ISODateString(new Date(d)));
return dojo.date.locale.format(d2, {selector: 'date', formatLength: 'long'});
}
function ISODateString(d) {
function pad(n){
return n<10 ? '0'+n : n
}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
看一下文档中的示例。我认为你只需要使用ISO格式的日期(yyyy-mm-dd),这通常是一个很好的做法。您也可以传入本机Date对象,但字符串显然更具可移植性。
那么,您需要的是WriteStore而不是ReadStore