CGBe
CGBe

Reputation: 183

Keep specific Datagrid row always at the end - also with Sorting

I'm stuck on following matter:

I have a datagrid with 10 items... I also added a extra row (row number 11) where I show a total of prevouis fields... But I always want to keep this last row (the totals) on the last line of the Datagrid (so always on position 11).

This means that when the datagrid is sorted on a column, the last row also changes in position according to the value in the datafield. Is there any easy and straightforward way of preventing it to be sorted with the other columns so that the column with the totals is always placed last? Or what approach would I use best?

Thanks for any help!

Upvotes: 0

Views: 844

Answers (2)

Mahesh Parate
Mahesh Parate

Reputation: 786

Please find below code Hope this may help you, i tried some workaround to achieve what you are looking for i am not sure how feasible is this code:-

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            [Bindable]
            private var dpHierarchy:ArrayCollection= new ArrayCollection([
                {name:"A", region: "Arizona", number:1},
                {name:"B", region: "Arizona", number:2},
                {name:"C", region: "California", number:3},
                {name:"D", region: "California", number:4}
            ]); 

            private function creationComp():void
            {
                var totalValue:int = 0;
                for(var i:int=0; i<dpHierarchy.length; i++)
                {
                    totalValue = totalValue + dpHierarchy[i].number;
                }
                var obj:Object = new Object();
                obj.name = "Total";
                obj.region = "==";
                obj.number = totalValue;
                dpHierarchy.addItem(obj)
                myADG.dataProvider = dpHierarchy;
            }

            private function sortHandler(obj1:Object, obj2:Object):int
            {
                var lastObj:Object = dpHierarchy.getItemAt(dpHierarchy.length-1);

                if(lastObj.number == obj1.number || lastObj.number == obj2.number)
                    return 0;

                if(obj1.number < obj2.number) {
                    return -1;
                } else if(obj1 == obj2) {
                    return 0;
                }
                return 1;
            }
        ]]>
    </fx:Script>

    <mx:AdvancedDataGrid id="myADG" x="50" y="50"
                         width="400" height="300" 
                         variableRowHeight="true" creationComplete="creationComp()">
        <mx:columns>
            <mx:AdvancedDataGridColumn dataField="name" headerText="Name" sortCompareFunction="sortHandler"/>
            <mx:AdvancedDataGridColumn dataField="region" headerText="Region" sortCompareFunction="sortHandler"/>
            <mx:AdvancedDataGridColumn dataField="number" headerText="Number" sortCompareFunction="sortHandler"/>
        </mx:columns>   

    </mx:AdvancedDataGrid>

</s:Application>

Upvotes: 1

Robert Smith
Robert Smith

Reputation: 385

I'd consider a few options in this order.

  1. Put your summary data somewhere else. That's really not what a row in a datagrid was intended for.
  2. Not let your users sort by clicking on the column name (sortableColumns='false') if you really don't want them to sort the data.
  3. Create a custom sort for each column that ensures you always put your summary data last. Here's a decent tutorial: http://www.switchonthecode.com/tutorials/flex-datagrid-custom-sorting. I'd like to stress that this would be a high maintenance idea because you'd have to build out a custom sort for every column.

Upvotes: 0

Related Questions