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就不需要转换了。
<?
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就不需要转换了。
相关文章推荐
- flex中datagrid显示格式化的日期可用labelFunction函数来处理
- C#中日期的各种格式化输出显示
- Asp.net日期字符串格式化显示方法[转]
- C#(ASP.NET)DateTime日期类型格式化显示 (转载)
- DateTime日期类型格式化显示
- Flex 日期格式化
- Asp.net日期字符串格式化显示
- Asp.net日期字符串格式化显示方法
- 有关日期类型格式化显示
- 格式化日期显示为2013-08-29 02:47:03的形式
- struts2中的jsp 格式化字符串显示成日期
- Asp.net日期字符串格式化显示方法
- 如何使用Javascript格式化日期显示
- C# DateTime日期类型格式化显示 zz
- 格式化日期显示
- 格式化日期时间的显示
- Asp.net日期字符串格式化显示方法[转]
- js jq插件 显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午 日期格式化
- flex显示数字时钟和日期
- Android 系列 2.10格式化显示的时间和日期