<?
xml?version
=
"
1.0
"
?encoding
=
"
utf-8
"
?>
<
mx:Application?xmlns:mx
=
"
http://www.adobe.com/2006/mxml
"
????????????????layout
=
"
vertical
"
????????????????creationComplete
=
"
initApp()
"
>
<
mx:Script
>
<!
[CDATA[
????
//
?On?startup
????
public
?function?initApp():
void
????{
????????
//
?Set?filter?function
????????
//
?Be?careful?to?set?filterFunction
????????
//
?only?after?ArrayCollection?has?been
????????
//
?populated.
????????myData.filterFunction
=
processFilter;
????}
????
//
?Filter?function
????
public
?function?processFilter(item:Object):Boolean
????{
????????var?result:Boolean
=
false
;
????????
//
?If?no?filter?text,?or?a?match,?then?true
????????
if
?(
!
item.name.length
????????????????
||
?item.name.toUpperCase().indexOf(txtFilter.text.toUpperCase())?
>=
?
0
)
????????result
=
true
;
????????
return
?result;
????}
]]
>
</
mx:Script
>
<!--
?Data?(use?ArrayCollection)?
-->
<
mx:ArrayCollection?id
=
"
myData
"
>
????
<
mx:source
>
????????
<
mx:Object?name
=
"
Ben?Forta
"
????????????location
=
"
Oak?Park,?MI
"
????????????phone
=
"
(248)555-5555
"
?
/>
????????
<
mx:Object?name
=
"
Jane?Doe
"
????????????location
=
"
New?York,?NY
"
????????????phone
=
"
(212)555-1234
"
?
/>
????????
<
mx:Object?name
=
"
Jim?Jones
"
????????????location
=
"
Atlanta,?GA
"
????????????phone
=
"
(414)555-1212
"
?
/>
????????
<
mx:Object?name
=
"
Roberta?Roberts
"
????????????location
=
"
Chicago,?IL
"
????????????phone
=
"
(312)555-4321
"
?
/>
????????
<
mx:Object?name
=
"
Steve?Stevens
"
????????????location
=
"
Boston,?MA
"
????????????phone
=
"
(617)555-5656
"
?
/>
????
</
mx:source
>
</
mx:ArrayCollection
>
<!--
?UI?
-->
<
mx:HBox?width
=
"
100%
"
>
????
<
mx:Label?text
=
"
Filter:
"
/>
????
<
mx:TextInput?id
=
"
txtFilter
"
????????????width
=
"
100%
"
????????????change
=
"
myData.refresh()
"
/>
</
mx:HBox
>
<
mx:DataGrid?dataProvider
=
"
{myData}
"
????????width
=
"
100%
"
?height
=
"
100%
"
>
????
<
mx:columns
>
????????
<
mx:DataGridColumn?headerText
=
"
Name
"
????????????????????dataField
=
"
name
"
/>
????????
<
mx:DataGridColumn?headerText
=
"
Location
"
????????????????????dataField
=
"
location
"
/>
????????
<
mx:DataGridColumn?headerText
=
"
Phone
"
????????????dataField
=
"
phone
"
/>
????
</
mx:columns
>
</
mx:DataGrid
>
</
mx:Application
>
其中主要的是ArrayCollection的filterFunction屬性,他的使用方法如下:
ArrayCollection的filterFunction屬性是繼承自ListCollectionView的,還有其他類具有這個(gè)功能,以下是一個(gè)繼承關(guān)系圖,詳細(xì)的可以看flex 的幫助文件
filterFunction屬性的值是一個(gè)函數(shù)(Function):
參數(shù):Object類型的一個(gè)值,也可以不帶參數(shù);
返回值:Boolean類型的值,如果返回值為T(mén)rue就把這個(gè)Object放到里面,反之亦然。
其函數(shù)格式事例如下:
f(item:Object):Boolean
在函數(shù)里面進(jìn)行處理,以上的例子就是如此:
?public function processFilter(item:Object):Boolean
?{
?????? var result:Boolean=false;
????? //?查看文本框里的字符串長(zhǎng)度或字符串的匹配(大小寫(xiě)都可以),然后返回結(jié)果。
??????if (!item.name.length || item.name.toUpperCase().indexOf(txtFilter.text.toUpperCase()) >= 0)
??? ?result=true;
??? ?return result;
?}
注意:filterFunction函數(shù)只有在對(duì)象建立的時(shí)候和調(diào)用reflash()的時(shí)候執(zhí)行的,所以一定要在顯示之前調(diào)用下reflash(),否則顯示就不正常了,切記!切記!
posted on 2007-01-23 10:21
???MengChuChen 閱讀(686)
評(píng)論(0) 編輯 收藏 所屬分類:
flex2.0