[flexcoders] Custom initial sort on datagrid

2005-12-30 Thread Clifford Hall



Hello all, I'm trying to get a datagrid to use a custom sortCompareFunction for its initial sort order.The custom sortCompareFunction works when you click the column header, however attempts to sort the dataprovider before it is assigned or the grid itself after the dataprovider is assigned all fail. The sortCompareFunction has been written so that it can sort either be passed objects as it would on an Array.sort (from which it will retrieve the string for comparison), or strings which the table will pass when the header is clicked.I have reproduced the various attempts to get it to work in a small mxml file included below. There are plenty of comments that show the various places that we may try to effect the sort. If you run this mxml, you'll see the table, and if you click the 'Age' column header, you'll see the sort work. If you are able to shed light on the error of my ways here and make this sort happen, I myself, my children and my children's children shall speak your name in the most reverant tones for generations to come, perhaps performing a little bedside ritual each night, ordering our pocket change from large to small around the base of the lamp before sleeping as a way of meditating upon the momentous occasion when The Answer was posted and the Great Difficulty passed.Thanks, -=Cliff?xml version="1.0" encoding="utf-8"?!-- Attempt to order the rows of a DataGrid by a column which requires a special sortCompareFunction --!-- This demo shows that the column (Age) sorts properly when the column header is clicked, --!-- but attempts to sort the dataprovider or the grid automatically at startup all fail --!-- Try assigning the dataprovider via binding, and tying a sort at creationComplete --mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" creationComplete="sortAtCreationComplete()"!-- Try assigning the dataprovider manually at startup, giving the opportunity to force a sort --!-- mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" creationComplete="manuallyAssignDP()" -- mx:Script ![CDATA[  import mx.utils.ArrayUtil;  import mx.utils.Delegate;   // sort order lookup hash  public var sortOrder:Array;public function sortAtCreationComplete()  {   // populate the sort order lookup hash   sortOrder=getSortOrder();// try to sort the datagrid   // dg1.sortItems(sortByAge,Array.ASCENDING) // try to sort the datagrid's dataprovider   dg1.dataProvider.sortItems(sortByAge, Array.ASCENDING)   }   public function manuallyAssignDP()  {   // populate the sort order lookup hash   sortOrder=getSortOrder();  var da:Array = filterData();// try sorting the dataprovider before assigning // should work but doesn't   // da.sortItems( sortByAge, Array.DESCENDING );   dg1.dataProvider = da;// try sorting the grid // should work but doesn't   // dg1.sortItems( sortByAge, Array.DESCENDING ); // ok, grasping at straws, here,// try passing the sort function via delegate doesn't work   // var d = new Delegate.create(this, sortByAge);   // dg1.sortItems( d, Array.DESCENDING ); // should   // would be lovely if we could do this,// since the age column has a sortCompareFunction,   // but alas, it never gets called, and the field   // is string sorted   // dg1.sortItemsBy('age', Array.DESCENDING); }  public function filterData():Array   {   var dp:Array = new Array;   dp.addItem( {hi:'abcd',  bye:'goodbye',  age:'young'} );   dp.addItem( {hi:'efgh',  bye:'zoob',  age:'younger still'} );   dp.addItem( {hi:'ijkl',  bye:'wappa',  age:'old'} );   dp.addItem( {hi:'mnop',  bye:'grrm',  age:'older'} );   dp.addItem( {hi:'qrst',  bye:'fluu',  age:'young'} );   dp.addItem( {hi:'uvwx',  bye:'norb',  age:'middle aged'} );   dp.addItem( {hi:'yyzz',  bye:'tepid',  age:'older yet'} );   dp.addItem( {hi:'AbCd',  bye:'wrrr',  age:'young'} );   dp.addItem( {hi:'eFgH',  bye:'grum',  age:'middle aged'} );   dp.addItem( {hi:'IjKl',  bye:'quixital', age:'young'} );   dp.addItem( {hi:'mNoP',  bye:'snorg',  age:'younger'} );  // try sorting the dataprovider here   // none of these work, though they should   //   // dp.sort(sortByAge);   // dp.sortItems(sortByAge);   // dp.sortItems(sortByAge, Array.DESCENDING);return dp; }   private function getSortOrder():Array  {   var so:Array = new Array();   so['younger still']   = 0;   so['younger'] = 1;   so['young'] = 2;   so['middle aged']= 3;   so['old']  = 4;   so['older'] = 5;   so['older yet']= 6;return so;}public function sortByAge( obj1bject, obj2bject, colIndex:Number ):Number  {   // depending on whether the method is being called as the// sortCompareFunction of a datagrid column, or as a call from   // sortItems on the table or array, it may be passed strings to   // compare, or objects from which we get the string, respectively  var o1:String;   var o2:String;   if (obj1.age != undefined) { o1 = obj1.age;o2 = obj2.age;   } else {o1 = 

RE: [flexcoders] Custom initial sort on datagrid

2005-12-30 Thread Matt Chotin










I think dg1.sortItems(sortByAge, asc)
should have worked (or desc), not Array.ASCENDING and
Array.DESCENDING because being consistent with array options would have been
too obvious ;-)



Havent tried it though. This will
be easier in Flex 2 because sorting will be done through Sort objects and
SortFields which the DG will recognize.



Matt











From:
flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Clifford Hall
Sent: Friday, December 30, 2005
10:42 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Custom
initial sort on datagrid





Hello all, 

I'm trying to get a datagrid to use a custom sortCompareFunction for its
initial sort order.

The custom sortCompareFunction works when you click the column header, however
attempts to sort the dataprovider before it is assigned or the grid itself
after the dataprovider is assigned all fail. The sortCompareFunction has been
written so that it can sort either be passed objects as it would on an
Array.sort (from which it will retrieve the string for comparison), or strings
which the table will pass when the header is clicked.

I have reproduced the various attempts to get it to work in a small mxml file
included below. There are plenty of comments that show the various places that
we may try to effect the sort. If you run this mxml, you'll see the table, and
if you click the 'Age' column header, you'll see the sort work. 

If you are able to shed light on the error of my ways here and make this sort
happen, I myself, my children and my chil! dren's children shall speak your
name in the most reverant tones for generations to come, perhaps performing a
little bedside ritual each night, ordering our pocket change from large to
small around the base of the lamp before sleeping as a way of meditating upon
the momentous occasion when The Answer was posted and the Great Difficulty
passed.

Thanks, 
-=Cliff

?xml version=1.0
encoding=utf-8?

!-- Attempt to order the rows of a DataGrid
by a column which requires a special sortCompareFunction --
!-- This demo shows that the column (Age)
sorts properly when the column header is clicked, --
!-- but attempts to sort the dataprovider or
the grid automatically at startup all fail --

!-- Try assigning the dataprovider via
binding, and tying a sort at creationComplete --
mx:Application
xmlns:mx=http://www.macromedia.com/2003/mxml creationComp!
lete=sortAtCreationComplete()

!-- Try assigni! ng the d ataprovider
manually at startup, giving the opportunity to force a sort --
!-- mx:Application
xmlns:mx=http://www.macromedia.com/2003/mxml
creationComplete=manuallyAssignDP() --

 mx:Script
 ![CDATA[
  import
mx.utils.ArrayUtil;
  import
mx.utils.Delegate;
 
  // sort
order lookup hash
  public var
sortOrder:Array;
  
  public
function sortAtCreationComplete()
  {
 
 // populate the sort order lookup hash
 
 sortOrder=getSortOrder();
 
  nb!
sp; // try to sort the datagrid
 
 //
dg1.sortItems(sortByAge,Array.ASCENDING) 
 
 
 // try to sort the datagrid's dataprovider
 
 dg1.dataProvider.sortItems(sortByAge,
Array.ASCENDING) 
  }
 
  public
function manuallyAssignDP()
  {
 
 // populate the sort order lookup hash
 
 sortOrder=getSortOrder();
 
 
 
 var da:Array = filterData();
 
nb! sp; 
 // try sorting the d! ataprovi der before
assigning  
 
 // should work but doesn't
 
 // da.sortItems( sortByAge, Array.DESCENDING ); 
 
 
 
 dg1.dataProvider = da;
 
 
 // try sorting the grid 

 
 // should work but doesn't
 
 // dg1.sortItems( sortByAge, Array.DESCENDING ); 
 
 
 // ok, grasping at straws, here, 
 
 // try passing the sort function via delegate doesn't work
nb! sp; 
 // var d = new Delegate.create(this, sortByAge);
 
 // dg1.sortItems( d, Array.DESCENDING ); // should 
 
 
 
 // would be lovely if we could do this, 
 
 // since the age column has a sortCompareFunction,
 
 // but alas, it never gets called, and the field
 
 // is string sorted
 
 // dg1.sortItemsBy('age', Array.DESCENDING);
 
 
  }
 
  
 !  public
function filterData():Arraynb! sp; 
  {
 
 var dp:Array = new Array;
 
 dp.addItem( {hi:'abcd',  bye:'goodbye',
 age:'young'} );
 
 dp.addItem( {hi:'efgh',  bye:'zoob',
 age:'younger still'} );
 
 dp.addItem( {hi:'ijkl',  bye:'wappa',
 age:'old'} );
 
 dp.addItem( {hi:'mnop',  bye:'grrm',
 age:'older'} );
 
 dp.addItem( {hi:'qrst',  bye:'fluu',
 age:'young'} );
 
 dp.addItem( {hi:'uvwx',  ! bye:'norb',
 age:'middle aged'} );
 
 dp.addItem( {hi:'yyzz',  bye:'tepid',
 age:'older yet'} );
 
 dp.addItem( {hi:'AbCd',  bye:'wrrr',
 age:'young'} );
 
 dp.addItem( {hi:'eFgH',  bye:'grum',
 age:'middle aged'} );
 
 dp.addItem( {hi:'IjKl',  bye:'quixital',
age:'young'} );
 
 dp.addItem( {hi:'mNoP',  bye:'snorg',
 age:'younger'} );
 
 
 
 // try sorting the dataprovider here
 ! 
 // none of these work, though ! they sho uld
 
 //
 
 // dp.sort(sortByAge);
 
 // dp.sortItems(sortByAge);
 
 // dp.sortItems(sortByAge, Array.DESCENDING);
 
 
 return dp;
 
 
 
}   
  
  private
function