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 ;-)
Haven’t 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:
[email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Clifford Hall
Sent: Friday, December 30, 2005
10:42 AM
To: [email protected]
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():Array &nb! 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 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;&! nbsp;
&n! bsp;&nbs p;
}
public
function sortByAge( obj1![]()
bject, obj2![]()
bject, 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;
&nb! sp;
var o2:String;
if (obj1.age != undefined) {
o1 = obj1.age;
o2 = obj2.age;
} else {
o1 = String(obj1);
o2 = String(obj2);
}
if ( sortOrder[ o1 ] == sortOrder[ o2 ] ) return 0;
if ( sortOrder[ o1 ] > sortOrder[ o2 ] ) return -1;
&nbs! p;
if ( sortOrder[ o1 ] < sortOrde! r[ o2 ] ) return 1;
return 0;
}
]]>
</mx:Script>
<!-- Try assigning the
dataprovider via binding, and tying a sort at creationComplete -->
<mx![]()
ataGrid id="dg1" height="250" width="100%"
sortableColumns="true" dataProvider="{filterData()}" >
<!-- Try assigning the
dataprovider manually at startup, giving the opportunity to force a sort -->
<!-- mx![]()
ataGrid id="dg1" height="250"
width="100%" sortableColumns="true" -->
<!-- also tried! setting
sortIndex and SortDirection on the grid -->
<!-- this placed the down
arrow on the age column, but never helps the data get sorted -->
<!--
sortIndex="0" sortDirection="Array.DESCENDING" -->
<mx:columns>
<mx:Array>
<mx![]()
ataGridColumn columnName="age"
sortable="true" sortCompareFunction="sortByAge"/>
<mx![]()
ataGridColumn columnName="hi" sortable="true"/>
&nb! sp;
<mx![]()
ataGridColumn columnName="bye"
sortable="true"/>
</mx:Array>
</mx:columns>
</mx![]()
ataGrid>
</mx:Application>
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
YAHOO! GROUPS LINKS