获取Flex DataGrid总行数的方法

Flex里面有一个public property:rowCount,那个是“获取或设置在列表中至少部分可见的行的行数”,并不能获得总的数据行。除此以外,好像没有什么属性、方法,可以直接获取DG总数据行数。在数据库里查找倒是很简单的。
  如果设置selectedIndex大于总的数据行数,那该索引指向的便是数据行的最后一行。由此便得到一个获取DG数据行的曲线方法。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
    <![CDATA[
    import mx.collections.SortField;
    import mx.collections.Sort;
    import mx.utils.ObjectUtil;
    import mx.collections.ArrayCollection;
    [Bindable]
    private var adg:ArrayCollection = new ArrayCollection([{name:"张三华",age:15,sex:"男"},
                                                           {name:"李四国",age:45,sex:"男"},
                                                           {name:"王七花",age:24,sex:"女"},
                                                           {name:"赵华柳",age:17,sex:"女"},
                                                           {name:"田三华",age:33,sex:"男"},]);
                                                          
                                                          
    private function init():void{
    adg.sort = new Sort();
    adg.sort.fields = [new SortField("age",false,true)];
    adg.refresh();
    dg.dataProvider = adg;
    }
    private function sortRanges(obj1:Object,obj2:Object):int{
    var value1:Number = obj1.age;
    var value2:Number = obj2.age;
    if(value1 > value2 ){
    return 1;
    }else if(value1 < value2 ){
    return -1;
    }else{
    return 0;
    }
    }            
    private function dataFilter():void{
    if(adg.filterFunction == null){
    adg.filterFunction = dataFilter2;
    }
    adg.refresh();
    }          
    private function dataFilter2(item:Object):Boolean{
    var bool:Boolean = true;
    var it:String = item["name"];
    var filter:String = ti.text;
    if(!it || it.toLowerCase().indexOf(filter.toLowerCase()) < 0){
    bool = false;
    }
    return bool;
    }      
    private function sortCompare(obj1:Object,obj2:Object):int{
    return ObjectUtil.numericCompare(obj1.age,obj2.age);
    }                
    private function test():void{
    var selectedRow:int;
var tempCount:int=1000;
var rCount:int;

selectedRow=dg.selectedIndex;

dg.selectedIndex=tempCount;
rCount=dg.selectedIndex+1;

dg.selectedIndex=selectedRow;
trace(rCount);
    }                
    ]]>
    </mx:Script>
    <mx:DataGrid id="dg"  x="89" y="76" width="552" height="160">
    <mx:columns>
    <mx:DataGridColumn  headerText="name" dataField="name"/>
    <mx:DataGridColumn headerText="age" dataField="age"/>
    <mx:DataGridColumn headerText="sex" dataField="sex" />
    </mx:columns>
    </mx:DataGrid>
    <mx:Form>
    <mx:FormItem>
    <mx:TextInput id="ti" change="dataFilter()"/>
    </mx:FormItem>
    <mx:FormItem>
    <mx:Button click="test()" label="show rowCount"/>
    </mx:FormItem>
    </mx:Form>
</mx:Application>

rCount就是总行数,因为Index是从0开始的,故此要加一。
tempCount设置是一个默认比行数多的数

你可能感兴趣的:(datagrid)