您的位置:首页 > 其它

Flex格式化显示日期

2010-04-08 09:20 330 查看
XML文件:

<?
xml
version
="1.0"
encoding
="iso-8859-1"
?>

<
books
>

<
stock
>

<
name
>
The Picasso Code</
name
>

<
author
>
Dan Blue</
author
>

<
category
>
Fiction</
category
>

<
description
>
Cubist paintings reveal a secret society of
people who really look like that</
description
>

<
date
>
2005-06-03</
date
>

</
stock
>

<
stock
>

<
name
>
Here With the Wind</
name
>

<
author
>
Margaret Middle</
author
>

<
category
>
Fiction</
category
>

<
description
>
In this edition, nobody in the south really
gives a damn</
description
>

<
date
>
2005-06-03</
date
>

</
stock
>

<
stock
>

<
name
>
Harry
Potluck and the Chamber of Money</
name
>

<
author
>
J.K. Roughly</
author
>

<
category
>
Fiction</
category
>

<
description
>
Young wizard finds the real pot-of-gold and
retires</
description
>

<
date
>
2005-06-03</
date
>

</
stock
>

<
stock
>

<
name
>
No
Expectations</
name
>

<
author
>
Chuck
Dickens</
author
>

<
category
>
Fiction</
category
>

<
description
>
Dickens
finally reveals what he really thinks of people</
description
>

<
date
>
2005-06-03</
date
>

</
stock
>

<
stock
>

<
name
>
Atlas Stretched</
name
>

<
author
>
Ann Rind</
author
>

<
category
>
Fiction</
category
>

<
description
>
Great inventors finally just take the money
and run</
description
>

<
date
>
2005-06-03</
date
>

</
stock
>

</
books
>

Flex源文件:

<?xml version="1.0"
encoding="utf-8"
?>

<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="bookData.send()">

<mx:Script>

<![CDATA[

import

mx.controls.dataGridClasses.DataGridColumn;

import
mx.rpc.events.*;

import

mx.collections.*;

import

mx.controls.*;

[Bindable]

private
var
bookStock:XMLList;

private
function

bookHandler(evt:ResultEvent):void

{

bookStock = evt.result.stock;

}

private
function

dateFormat(dateItem:Object,date:DataGridColumn):String{

trace(publishDate.format(String(dateItem.date)));

return

publishDate.format(String(dateItem.date));

}

]]>

</mx:Script>

<mx:HTTPService
id="bookData"
url="./books.xml"
result="bookHandler(event)"
resultFormat="e4x"
/>

<mx:DateFormatter id="publishDate"
formatString="MMMM/DD/YYYY"
/>

<mx:DataGrid x="104"
y="38"
width="638"

height="100%"
dataProvider="{bookStock}"
variableRowHeight="true"
>

<mx:columns>

<mx:DataGridColumn
headerText="name"
dataField="name"
/>

<mx:DataGridColumn
headerText="Date"
dataField="date"
labelFunction="dateFormat"
/>

</mx:columns>

</mx:DataGrid>

</mx:Application>


格式化字符串为“MMMM/DD/YYYY


的时候,格式化后的日期为June/03/2005,格式化字符串为“MMM/DD/YYYY

”的时候,格式化后的日期也为June/03/2005,而当格式
化字符串为“MM/DD/YYYY


的时候,格式化后的日期就变成了06/03/2005。

还有一个要注意的地方就是:

return
publishDate.format(String(dateItem.date));

刚开始的时候我是这样
写的:

return
publishDate.format(dateItem.date);


试的时候,日期那一列总是显示为空,找了半天也没找出原因,后来我用trace调试

trace(publishDate.format("2005-06-03"));

发现是可以显示的,在调试模式下,跟踪了一下dateItem.date的数据类型,
结果是XMLList格式的,这是才发现我上面取XML数据的时候用的是XMLList类型,dateItem.date得到的结果就不是字符串,所以无
法格式化,因此要强制转换成String格式的。如果
DataGrid
使用的是bookData.lastResult获得数据,或者
bookStock
的类
型是ArrayCollection就不需要转换了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: