DataGrid的过滤

Posted on 2009-02-20 15:44 追梦人 阅读(284) 评论(0)  编辑  收藏 所属分类: flex

在filter input 输入字符,可对DataGrid进行过滤。道理很简单,就是利用自定义的filterFunction给DataGrid进行过滤。

直接来看代码

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

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:local="*" backgroundColor="#999999">

      <mx:Script>

            <![CDATA[

                  import mx.collections.XMLListCollection;

                  [Bindable]

                  private var resultText:String = "Result(s)";

                  private var xml:XML = <root>

                                                <c firstName="Michael" lastName="Scofield" address="Fox river"/>

                                                <c firstName="Lincoln" lastName="Burrows" address="Sona"/>

                                                <c firstName="Fernando" lastName="Sucre" address="Boston"/>

                                                <c firstName="Theodore T-Bag" lastName="Bagwell" address="Houston"/>

                                                <c firstName="Veronica" lastName="Donovan" address="New York"/>

                                                <c firstName="Henry" lastName="Pope" address="Miami"/>

                                                <c firstName="LJ" lastName="Burrows" address="Phoenix"/>                                                                 </root>;

                 

                  [Bindable]

                  private var dp:XMLListCollection = new XMLListCollection(xml.children());

                 

                  private function textChangeHandler():void{

                        if(textInput.length==0)

                        {

                              dp.filterFunction = null;

                        }

                        else

                        {

                              dp.filterFunction = filterFunction;

                        }

                        dp.refresh();

                        resultText = dp.length + " Result(s)";

                  }

                 

                  private function filterFunction(item:Object):Boolean

                  {          

                        var searchString:String = textInput.text.toLocaleLowerCase();                      

                        var tempString : String;

                        for(var j:int = 0; j<apps.columns.length;j++) {

                              tempString = String (item[apps.columns[j].dataField]);                             

                              if(tempString.toLocaleLowerCase().indexOf(searchString)>-1) return true;

                        }                

                        return false;

                  }

            ]]>

      </mx:Script>

      <mx:HBox width="100%">

            <mx:Label text="Filter Input:"/>

            <mx:TextInput id="textInput" change="textChangeHandler()"/>

            <mx:Spacer width="100%"/>

            <mx:Label text="{resultText}"/>          

      </mx:HBox>

      <mx:DataGrid id="apps" dataProvider="{dp}" width="100%">

            <mx:columns>

                  <mx:DataGridColumn dataField="@firstName" headerText="First_Name"/>

                  <mx:DataGridColumn dataField="@lastName" headerText="Last_Name"/>

                  <mx:DataGridColumn dataField="@address" headerText="Address"/>

            </mx:columns>

      </mx:DataGrid>     

</mx:Application>


只有注册用户登录后才能发表评论。


网站导航: