views:

1569

answers:

7

Hi,

I need to to a programmatic multi column sorting on the AdvancedDataGrid. The issue is, currently I am implementing paging on my grid. So, if I sort the data, only the perticular page is being sorted. So, I need to sort the entire list by the column criteria.

I tried HeaderRelease event, but I guess it is of no use as I need to make a round trip call to the server to get the sorted data. Is there any way I can implement that. I also need to show the sort markers in the Column headers' right position, indicating the sort numbers and direction.

'll really appreciate the help

Thanks :)

A: 

If your data is paged, you'll have to go to the server to retrieve the data anyways. If only a portion of the data is stored client side, it might be better to just sort on the server, since only the server will have all the data.

CookieOfFortune
Yes. I got that. But, how should I go ahead with sorting on the server when the header is clicked? Moreover, I also need to provide the multiple column sort as well. Along with these, I need to update columns' sort area with the sort order number as well as the sort direction. 'll really appreciate help in this regards.
online19
I think you're going to have to override the column header sorting, since it doesn't apply in your case at all because you always go back to the server. If I were doing it... I would probably have an array that keeps track of the columns you want sorted. Send that to the server to be incorporated into the SQL query or whatever you're using, and the server will respond with the correct dataset.
CookieOfFortune
Yes, that is what I am targeting. But, the sorting is dynamic. As in, I am not sure how to keep track of the columns to be sorted as there are lots of possibilities. 1.Single sort on any of the columns, 2. multiple sort on combination of several columns, etc.So, I am confused as to where I can trace this. On the header release event or on the sort event?Please help. Thanks :)
online19
I would probably intercept the HeaderReleased event, as that occurs before sorting. Then I would call preventDefault() to stop the normal sorting action. How did you want to implement the multi-column sorting? The method I was thinking about was using a stack (Array). Everytime a sort column is requested, push that column name to the bottom of the stack. Then, when you call your server to sort, it will sort the data using the columns in the stack. So the first column clicked would have the highest priority and the last column would have the lowest. Nope that preventDefault also prevents
CookieOfFortune
A: 

You need to extend the AdvancedDataGrid and override the 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(',');
}
Lastly, in your sortHandler method, dispatchEvent('GOTOSERVERNOW') Handle the event outside ADG and construct your query with ADG.orderBy and now fetch new set of records.
A: 

I think we need to extend the AdvancedDataGrid and overwrite the sortHandler. I'm doing this currently. However, I'm facing a lot of problems with multisort and sortorder. Which is normal of course, after a sort I query the database and update my dataprovider which also loses the list of sortitems...

So first sort works, but suppose I want a second sortfield, it is handled as a first sortfield again.

Does anyone have an idea how to fix this??

Thanks.

CookieOfFortune, did you actually manage to implement this with the header_release. I think it's a lot more work...

A: 
A: 

SORRY Guys!! Missed the inital part of the code.... Here is the right code

<: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>
A: 

You can also have a look at the AdvancedDataGridSortItemRenderer class. This helps to customise the sort item renderer.

Thanks, Shameer

Shameer Salim
A: 

Try modifiyng the event priority of the sort event, no need to override the sort handler, but can't be done in 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}" />

bas