Jul 12
接下来的例子演示了Flex中如何通过filterFunction属性,对XMLListCollection进行筛选。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
- <mx:Script>
- <![CDATA[
- private function filterFunc(item:Object):Boolean {
- return item.text().match(new RegExp("^" + stateName.text, "i"));
- }
- ]]>
- </mx:Script>
- <mx:XML id="dp" source="countries_states.xml" format="e4x" />
- <mx:XMLListCollection id="xmlListColl" source="{dp.country.(@name == 'United States of America').state}" filterFunction="filterFunc" />
- <mx:VBox>
- <mx:HBox width="100%">
- <mx:Label text="Name:" />
- <mx:TextInput id="stateName" width="100%" change="xmlListColl.refresh()" />
- </mx:HBox>
- <mx:DataGrid id="dataGrid" dataProvider="{xmlListColl}">
- <mx:columns>
- <mx:DataGridColumn id="codeCol" dataField="@code" headerText="Abbr:" width="60" />
- <mx:DataGridColumn id="nameCol" dataField="*" headerText="Name:" width="240" />
- </mx:columns>
- </mx:DataGrid>
- <mx:Label text="Filtered: Showing {xmlListColl.length} record(s)" visible="{stateName.text.length > 0}" />
- </mx:VBox>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer
