扩展Flex DataGridColumn以进行自定义排序功能

时间:2009-08-25 20:13:43

标签: flex datagrid adobe

我扩展了DataGridColumn,因为我想要包含一个自定义itemToLabel函数(为了能够在DataGrid中显示嵌套数据。请参阅this问题。

无论如何,它还需要一个自定义排序功能。所以我写了这样的排序函数:

private function mySortCompareFunction(obj1:Object, obj2:Object):int{
    var currentData1:Object = obj1;
    var currentData2:Object = obj2;

    //some logic here to get the currentData if the object is nested.

    if(currentData1 is int && currentData2 is int){
        var int1:int = int(currentData1);
        var int2:int = int(currentData2);
        var result:int = (int1>int2)?-1:1; 
        return result;
    }
    //so on for string and date
}

在我的CustomDataGridColumn的构造函数中,我放了:     超级(COLUMNNAME);     sortCompareFunction = mySortCompareFunction;

每当我尝试对列进行排序时,都会收到错误“Error: Find criteria must contain at least one sort field value.

当我调试并逐步执行每一步时,我看到前几次,函数被正确调用,但最后,会发生此错误。

有人可以对这里发生的事情有所了解吗?

感谢。

4 个答案:

答案 0 :(得分:1)

我也看到了这个错误,我将其追踪到其中一个包含'null'的单元格。 如果我没记错的话,当其中一列的'dataField'属性错误时,也会出现此错误。

HTH,

Koen Weyn

答案 1 :(得分:1)

只是为了说明我是如何解决这个问题的(为了他人的利益):

我没有使用dataField属性(因为我从嵌套对象获取数据,因此我将data.namedata.title指定给我),我创建了自己的字段{{ 1}}并使其可绑定。所以我的代码现在基本上看起来像这样:

nestedDataField

然后我将这个新变量分配给我的dataField条目

public class DataGridColumnNested extends DataGridColumn{
[Bindable] public var nestedDataField:String;

private function mySortCompareFunction(obj1:Object, obj2:Object):int{
    var currentData1:Object = obj1;
    var currentData2:Object = obj2;

    //some logic here to get the currentData if the object is nested.

    if(currentData1 is int && currentData2 is int){
        var int1:int = int(currentData1);
        var int2:int = int(currentData2);
        var result:int = (int1>int2)?-1:1; 
        return result;
    }
    //so on for string and date
}
}

瞧瞧!它没有任何障碍。

答案 2 :(得分:1)

我经常发现使用标准数据字段更容易,只需在我的valueobject中编写一个getter函数即可用作数据字段。 例如:

//file SomeObject.as with a nested object as property
public class SomeObject
{
   public var someProperty:AnotherObject;

   public function get someString():String;
   {
     if(someProperty)
        return someProperty.someString;
     else
        return "";
   }
}

//your nested class, AnotherObject.as
public class AnotherObject
{
   public var someString:String;
}

//this way sorting and the label will work without custom label/compare functions
<mx:DataGridColumn headerText="" dataField="someString"/>

答案 3 :(得分:1)

解决问题的最简单方法是使用labelFunction更改dataField =“obj.atributte”。如果您愿意,也可以添加sortCompareFunction。

    <mx:DataGridColumn headerText="email" 
dataField="user.email" textAlign="center" />

改变

    <mx:DataGridColumn headerText="email" 
labelFunction="emailLabelFunction" 
sortCompareFunction="emailsCompareFunction2" 
textAlign="center" />



public static function emailLabelFunction(item:Object, column:DataGridColumn):String{

   if (item!=null){
    var user:User = (item as TpAnnouncementUser).user as User;

    return user.email;
   }else{

    return "";
   }
}

public static function emailCompareFunction(obj1:Object, obj2:Object):int{

    var dato1:String = User(obj1).email.toLowerCase();
    var dato2:String = User(obj2).email.toLowerCase();

    return ObjectUtil.compare(dato1, dato2);

}


    public static function emailsCompareFunction2(obj1:Object, obj2:Object):int{

    var dato3:User = (TpAnnouncementUser(obj1).user as User);
    var dato4:User = (TpAnnouncementUser(obj2).user as User);

    return emailCompareFunction(dato3, dato4);