5 Replies Latest reply on Jul 31, 2007 6:10 PM by SwaroopAdobe

    sort column in advanceddatagrid

    Roman Trushkov
      Hi, i have an interesting question - how to sort column id datagrid, like i clicked on a header. I use AdvancedDataGrid and HierarchialData
        • 1. Re: sort column in advanceddatagrid
          SwaroopAdobe Level 1
          Hi Roman,

          Sorting should already work for HierarchicalData.

          Do you have a case where it doesn't work? If so, what exactly are you trying to achieve?

          Thanks,
          Swaroop
          • 2. sort column in advanceddatagrid
            Roman Trushkov Level 1
            Yes, it works for HierarchicalCollectionView, but works for only first level of the tree. Like I sort an XMLListCollection, comparefunction handles only full elements of the first level. I need also to sort children nodes of the thee (customer wants).

            I have done it for XMLListCollection which is the source of HierarchialData but it's too many letters =)) and i need sort it and refresh HierarchialData after every change. Advanced datagrid makes it automatically by pressing on a header.

            Here is the code:
            quote:


            public static function sortXMLListCollection(xml:XMLListCollection,dataField:String):XMLListCollection
            {
            var sorting:Sort=new Sort();
            sorting.fields=[new SortField(dataField)];
            xml.sort=sorting;
            xml.refresh();

            for each(var item:XML in xml)
            {
            XMLUtils.sortXMLNode(item,sorting);
            }

            return xml;
            }
            public static function sortXMLNode(node:XML,sorting:Sort):XML
            {
            var tempXML:XMLListCollection=new XMLListCollection(node.*);
            tempXML.sort=sorting;
            tempXML.refresh();
            XMLUtils.clearNode(node);
            XMLUtils.appendXMLListInXML(node,tempXML.copy());
            for each(var item:XML in node.*)
            {
            XMLUtils.sortXMLNode(item,sorting);
            }
            return node;
            }
            public static function appendXMLListInXML(xml:XML,xmlList:XMLList):XML
            {
            for each(var appendXML:XML in xmlList)
            {
            xml.appendChild(appendXML)
            }
            return xml;
            }
            public static function clearNode(xml:XML):XML
            {
            for(var i:int=new XMLListCollection(xml.*).length-1;i>=0;i--)
            {
            delete xml.* ;
            }
            return xml;
            }

            • 3. Re: sort column in advanceddatagrid
              sameerb Level 1
              Hi,
              Sorting in Hierarchical data should work by default without using any compare Function.
              Try clicking the header and check the results, they will be sorted recursively.

              For more on sorting different levels separately, check out -
              http://techrays.wordpress.com/2007/07/18/adding-flexibility-to-sorting-hierarchical-data/


              Thanks,
              Sameer
              • 4. Re: sort column in advanceddatagrid
                Roman Trushkov Level 1
                Yes, it works when i click on it, i needed a tree without a header sorted automatically, and i needed to "click" on it from flex (i mind programmely, from the code). I found a solution, thanks.

                quote:


                <?xml version="1.0"?>
                <!-- dpcontrols/DataGridEvents.mxml -->
                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
                <mx:Script>
                <![CDATA[
                import mx.events.AdvancedDataGridEvent;

                private function sortNow(colIndex:int=0):void{
                var dev:AdvancedDataGridEvent = new AdvancedDataGridEvent(AdvancedDataGridEvent.HEADER_RELEASE);
                dev.columnIndex = colIndex;
                dev.dataField = myGrid.columns[colIndex].dataField;
                myGrid.dispatchEvent(dev);
                }
                ]]>
                </mx:Script>
                <mx:AdvancedDataGrid id="myGrid" width="350" height="150" >
                <mx:ArrayCollection>
                <mx:Object Country="Украина" City="Киев" People="3 млн." />
                <mx:Object Country="Россия" City="Москва" People="8 млн." />
                </mx:ArrayCollection>
                </mx:AdvancedDataGrid>

                <mx:Button label="Отсортировать" click="sortNow();"/>

                </mx:Application>

                • 5. Re: sort column in advanceddatagrid
                  SwaroopAdobe Level 1
                  Hey Roman,

                  In this particular use case, it would be better to dispatch the AdvancedDataGridEvent.SORT event instead of the headerReleaseHandler. Using the SORT event will allow you to make use of the special flags for adding and removing from multi column sorting.

                  Thanks,
                  Swaroop