Flex 4.5 DataGrid 绑定Xml方法

      从Flex4 后,Datagrid 组件就已经由原来的mx 换成spark.components.DataGrid 。原来mx下的DataGrid下可以直接绑定xml,但到了spark. 下的dataProvider 是需要实现Ilist接口的。整理后代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- dpcontrols\sparkdpcontrols\SparkDGXMLData.mxml -->
<s:Application
	xmlns:fx="http://ns.adobe.com/mxml/2009"
	xmlns:s="library://ns.adobe.com/flex/spark"
	xmlns:mx="library://ns.adobe.com/flex/mx"
	width="500">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<fx:Declarations>
		<fx:XMLList id="employees">
			<employee>
				<name>Joanne Wall</name>
				<phone>555-219-2012</phone>
				<email>jwall@fictitious.com</email>
				<active>true</active>
			</employee>            
			<employee>
				<name>Mary Jones</name>
				<phone>555-219-2000</phone>
				<email>mjones@fictitious.com</email>
				<active>true</active>
			</employee>            
			<employee>
				<name>Maurice Smith</name>
				<phone>555-219-2012</phone>
				<email>maurice@fictitious.com</email>
				<active>false</active>
			</employee>            
			<employee>
				<name>Dave Davis</name>
				<phone>555-219-2000</phone>
				<email>ddavis@fictitious.com</email>
				<active>true</active>
			</employee>            
			<employee>
				<name>Tom Maple</name>
				<phone>555-219-2000</phone>
				<email>tmaple@fictitious.com</email>
				<active>true</active>
			</employee>            
		</fx:XMLList>
		<s:XMLListCollection id="employees2" source="{employees}"/>
	</fx:Declarations>
	
	<s:DataGrid id="dg" width="500" dataProvider="{employees2}">
		<s:columns>
			<s:ArrayList>
				<s:GridColumn dataField="name" headerText="Name"/>
				<s:GridColumn dataField="phone" headerText="Phone"/>
				<s:GridColumn dataField="email" headerText="Email"/>
			</s:ArrayList>
		</s:columns>
	</s:DataGrid>
	
	<s:Form>
		<s:FormItem label="Name">
			<s:Label text="{dg.selectedItem.name}"/>
		</s:FormItem>
		<s:FormItem label="Email">
			<s:Label text="{dg.selectedItem.email}"/>
		</s:FormItem>
		<s:FormItem label="Phone">
			<s:Label text="{dg.selectedItem.phone}"/>
		</s:FormItem>
	</s:Form>
</s:Application>

 

   另外:如果XML数据源不是这种标准格式而是以Atturibe 呈现时,绑定字段时应以@连接,则应该做相应修改:

  

<?xml version="1.0" encoding="utf-8"?>
<!-- dpcontrols\sparkdpcontrols\SparkDGXMLData.mxml -->
<s:Application
	xmlns:fx="http://ns.adobe.com/mxml/2009"
	xmlns:s="library://ns.adobe.com/flex/spark"
	xmlns:mx="library://ns.adobe.com/flex/mx"
	width="500">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<fx:Declarations>
		<fx:XMLList id="employees">
			<employee name="Joanne Wall" phone="555-219-2012" email="jwall@fictitious.com" active="true" />                     
		</fx:XMLList>
		<s:XMLListCollection id="employees2" source="{employees}"/>
	</fx:Declarations>
	
	<s:DataGrid id="dg" width="500" dataProvider="{employees2}">
		<s:columns>
			<s:ArrayList>
				<s:GridColumn dataField="@name" headerText="Name"/>
				<s:GridColumn dataField="@phone" headerText="Phone"/>
				<s:GridColumn dataField="@email" headerText="Email"/>
			</s:ArrayList>
		</s:columns>
	</s:DataGrid>
	
	<s:Form>
		<s:FormItem label="Name">
			<s:Label text="{dg.selectedItem.@name}"/>
		</s:FormItem>
		<s:FormItem label="Email">
			<s:Label text="{dg.selectedItem.@email}"/>
		</s:FormItem>
		<s:FormItem label="Phone">
			<s:Label text="{dg.selectedItem.@phone}"/>
		</s:FormItem>
	</s:Form>
</s:Application>

 

效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值