AdvancedDataGrid多列编程排序

时间:2009-07-30 15:19:40

标签: flex flex3 advanceddatagrid

我需要对AdvancedDataGrid进行编程多列排序。 问题是,目前我正在我的网格上实现分页。因此,如果我对数据进行排序,则只对特定页面进行排序。因此,我需要按列标准对整个列表进行排序。

我尝试了HeaderRelease事件,但我想这没用,因为我需要对服务器进行往返调用以获取已排序的数据。有什么办法可以实现吗?我还需要在列标题的右侧位置显示排序标记,指示排序编号和方向。

非常感谢帮助

谢谢:)

6 个答案:

答案 0 :(得分:1)

您需要扩展AdvancedDataGrid并覆盖sortHandler

public var orderBy:String; 
override protected function sortHandler(event:AdvancedDataGridEvent):void {
     super.sortHandler(event); 
     var arry:Array = [];  
     for each(var o:SortField in collection.sort.fields){
         arry.push(o.name+' '+(o.descending?'DESC':'ASC'));     
     }
     orderBy = arry.join(',');
}

答案 1 :(得分:0)

如果您的数据被分页,您将不得不去服务器检索数据。如果只有一部分数据存储在客户端,那么在服务器上排序可能会更好,因为只有服务器才能拥有所有数据。

答案 2 :(得分:0)

创建您自己的自定义Advance Datagrid,如下例所示。 您有已排序列的数组和顺序。发送cutom事件并将此数组传递给它。服务器端Tech将相应地进行查询并返回结果。

             

        import mx.events.CollectionEvent;
        import mx.binding.utils.BindingUtils;
        import mx.collections.SortField;
        import mx.collections.Sort;
        import mx.collections.ArrayCollection;
        import mx.events.AdvancedDataGridEvent;

        private var sortOrder:Boolean = true;
        [Bindable]
        public var headerHt:int = 30;


        [Inspectable(defaultValue="true", type="Boolean", enumeration="true,false", inherit="yes")]
        [Bindable]
        public var allowHeaderWordWrap:Boolean = true;

        public function sortCaseInsensitive(event:AdvancedDataGridEvent):void{
            var genericDataProvider:Object  = this.dataProvider as Object;
            sortOrder = !sortOrder; 
            //genericDataProvider.sort = new Sort();
            if(genericDataProvider.sort == null){
                genericDataProvider.sort = new Sort();
            } 
            var sortField:SortField = new SortField(event.dataField,true,sortOrder);
            switch (event.dataField) {
                  case "assmtId":
                      sortField.numeric = true;
                break;

            }//switch

            //genericDataProvider.sort.fields = [sortField];
            if(genericDataProvider.sort.fields == null){
                genericDataProvider.sort.fields = [sortField];
            }else{
                //if dataField is not already present in sort fields array 
                if(genericDataProvider.sort.fields.indexOf(sortField) != -1){
                    genericDataProvider.sort.fields = genericDataProvider.sort.fields.concat(sortField);
                }
             }
            genericDataProvider.refresh();

                 // Send custom event to server and pass the array of sort to it the server side technology will make dynamic query and return the result
        }//sortCaseInsensitive

    ]]>
</mx:Script>

答案 3 :(得分:0)

SORRY伙计们!!错过了代码的初始部分....这是正确的代码

<:AdvancedDataGrid headerWordWrap="{allowHeaderWordWrap}"   headerRelease="sortCaseInsensitive(event)" headerHeight="{headerHt}">

    <:Script>
        <[CDATA[
            import mx.events.CollectionEvent;
                import mx.binding.utils.BindingUtils;
                import mx.collections.SortField;
                import mx.collections.Sort;
                import mx.collections.ArrayCollection;
                import mx.events.AdvancedDataGridEvent;

                private var sortOrder:Boolean = true;
                [Bindable]
                public var headerHt:int = 30;


                [Inspectable(defaultValue="true", type="Boolean", enumeration="true,false", inherit="yes")]
                [Bindable]
                public var allowHeaderWordWrap:Boolean = true;

                public function sortCaseInsensitive(event:AdvancedDataGridEvent):void{
                var genericDataProvider:Object  = this.dataProvider as Object;
                        sortOrder = !sortOrder; 
                        //genericDataProvider.sort = new Sort();
                        if(genericDataProvider.sort == null){
                                genericDataProvider.sort = new Sort();
                        } 
                        var sortField:SortField = new SortField(event.dataField,true,sortOrder);
                switch (event.dataField) {
                      case "assmtId":
                      sortField.numeric = true;
                break;

                }//switch

                //genericDataProvider.sort.fields = [sortField];
                if(genericDataProvider.sort.fields == null){
                        genericDataProvider.sort.fields = [sortField];
                }else{
                        //if dataField is not already present in sort fields array 
                        if(genericDataProvider.sort.fields.indexOf(sortField) != -1){
                                genericDataProvider.sort.fields = genericDataProvider.sort.fields.concat(sortField);
                        }
                 }
            genericDataProvider.refresh();

                 // Send custom event to server and pass the array of sort to it the server side technology will make dynamic query and return the result
                }//sortCaseInsensitive

        ]]>
<:Script>
<:AdvancedDataGrid>

答案 4 :(得分:0)

您还可以查看AdvancedDataGridSortItemRenderer课程。这有助于自定义排序项呈示器。

答案 5 :(得分:0)

尝试修改sort事件的事件优先级,不需要覆盖排序处理程序,但不能在mxml中完成。


protected function dataGrid_initializeHandler(event:FlexEvent):void {
    dataGrid.addEventListener(AdvancedDataGridEvent.SORT, dataGrid_sortHandler, false, -50);
}

protected function dataGrid_sortHandler(event:FlexEvent):void {
    dataGrid.dataProvider.sort; // now up to date!
}


<mx:AdvancedDataGrid id="dataGrid" initialize="dataGrid_initializeHandler(event)" dataProvider="{model.dataProvider}" />