[flexcoders] Help trying to sort data provider for DataGrid

2009-08-20 Thread lanekelly5
I'm trying to load a few elements of an RSS feed into a DataGrid, and I'm 
having trouble with the code to pre-sort the data before the grid gets it.  
Here's a snippet of the code:

private function requestRSS(e:FlexEvent):void
{
initVars();
showBusyCursor();
rssService.url = rssUrl;
rssService.resultFormat = e4x;
rssService.useProxy = false;
rssService.addEventListener(ResultEvent.RESULT, 
resultHandler);
rssService.addEventListener(FaultEvent.FAULT, 
faultHandler);
rssService.send();
}

private function resultHandler(e:ResultEvent):void
{
listHeadlines.dataProvider = 
e.target.lastResult.channel.item;
}

In the mxml:
mx:DataGrid id=listHeadlines width=433 height=113 
click=showDetails(event)
mx:columns
mx:DataGridColumn dataField=pubDate sortDescending=true/
mx:DataGridColumn dataField=title/
/mx:columns
/mx:DataGrid 

At this point my data in e.target.lastResult.channel.item is not always sorted 
properly.  I'd like to sort it by pubDate descending before filling out the 
DataGrid.  Any guidance on this?



Re: [flexcoders] Help trying to sort data provider for DataGrid

2009-08-20 Thread Wesley Acheson
I've not experienced it yet my self but the problem is probably the dates.
Flex does string comparison on the dates not object comparison. (even if
they were dates which they're probably strings by the time they get into
your application.  I haven't verified the accuracy of said post but at a
glance
http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/looks
like it has what you need.

On Thu, Aug 20, 2009 at 9:30 PM, lanekelly5 lkel...@gmail.com wrote:

 I'm trying to load a few elements of an RSS feed into a DataGrid, and I'm
 having trouble with the code to pre-sort the data before the grid gets it.
  Here's a snippet of the code:

 private function requestRSS(e:FlexEvent):void
{
initVars();
showBusyCursor();
rssService.url = rssUrl;
rssService.resultFormat = e4x;
rssService.useProxy = false;

  rssService.addEventListener(ResultEvent.RESULT, resultHandler);

  rssService.addEventListener(FaultEvent.FAULT, faultHandler);
rssService.send();
}

 private function resultHandler(e:ResultEvent):void
{
listHeadlines.dataProvider =
 e.target.lastResult.channel.item;
}

 In the mxml:
 mx:DataGrid id=listHeadlines width=433 height=113
 click=showDetails(event)
mx:columns
mx:DataGridColumn dataField=pubDate sortDescending=true/
mx:DataGridColumn dataField=title/
/mx:columns
 /mx:DataGrid

 At this point my data in e.target.lastResult.channel.item is not always
 sorted properly.  I'd like to sort it by pubDate descending before filling
 out the DataGrid.  Any guidance on this?



 

 --
 Flexcoders Mailing List
 FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
 Alternative FAQ location:
 https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847
 Search Archives:
 http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups
 Links






RE: [flexcoders] Help trying to sort data provider for DataGrid

2009-08-20 Thread Tracy Spratt
If it is not a date format issue then you might want to put the XML list
into a collection array and sort that.  If you want to keep the xml (as I
often do), use setChildren to assign the sorted XML list back to the root
xml.

 

If you do not need to keep the xml (as I sometimes do not) Put the data into
an ArrayCollection of strongly typed value objects.  This has other benefits
as well.

 

Below is a function I use to sort XML child nodes.

 

/** Sorts an xml node's children on a single attribute

*   Returns a copy of the xml node with the children in the sorted order
*/

public static function sortChildren(xml:XML, 

  sAttrName:String, 

  bCaseInsensitive:Boolean=true, 

  bDescending:Boolean=true):XML

{

  var xmlReturn:XML;

  if (xml != null)  {

xmlReturn = xml.copy();//copy to get the root node.  We will
replace the children

xml = xml.copy();  //copy so we can append the
children to the return xml directly

var xlcChildren:XMLListCollection = new
XMLListCollection(xml.children());

var xlChildren:XMLList;

var xlcSorted:XMLListCollection = new XMLListCollection();

var sort:Sort = new Sort();// Create the Sort instance.

sort.fields = [new
SortField(sAttrName,bCaseInsensitive,bDescending)];  // Both fields are
case-insensitive.

xlcChildren.sort = sort;  // Assign the Sort object to the
collection.

xlcChildren.refresh();// Apply the sort to the
collection.

for (var i:int=0;ixlcChildren.length;i++)  {  //loop over the
children in index order

  xlcSorted.addItemAt(xlcChildren.getItemAt(i),i); //add the node to
the new collection

}

xlChildren = xlcSorted.source; //get the XMLlist from the source
property 

xmlReturn.setChildren(xlChildren); //set the return xml children.


  }

  return xmlReturn;

}//sortChildren

 

Tracy Spratt,

Lariat Services, development services available

  _  

From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On
Behalf Of Wesley Acheson
Sent: Thursday, August 20, 2009 4:04 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Help trying to sort data provider for DataGrid

 

  

I've not experienced it yet my self but the problem is probably the dates.
Flex does string comparison on the dates not object comparison. (even if
they were dates which they're probably strings by the time they get into
your application.  I haven't verified the accuracy of said post but at a
glance http://blog.
http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/
 flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/ looks like
it has what you need.

On Thu, Aug 20, 2009 at 9:30 PM, lanekelly5 lkel...@gmail.
mailto:lkel...@gmail.com com wrote:

I'm trying to load a few elements of an RSS feed into a DataGrid, and I'm
having trouble with the code to pre-sort the data before the grid gets it.
Here's a snippet of the code:

private function requestRSS(e:FlexEvent):void
   {
   initVars();
   showBusyCursor();
   rssService.url = rssUrl;
   rssService.resultFormat = e4x;
   rssService.useProxy = false;
 
rssService.addEventListener(ResultEvent.RESULT, resultHandler);
   rssService.addEventListener(FaultEvent.FAULT,
faultHandler);
   rssService.send();
   }

private function resultHandler(e:ResultEvent):void
   {
   listHeadlines.dataProvider =
e.target.lastResult.channel.item;
   }

In the mxml:
mx:DataGrid id=listHeadlines width=433 height=113
click=showDetails(event)
   mx:columns
   mx:DataGridColumn dataField=pubDate sortDescending=true/
   mx:DataGridColumn dataField=title/
   /mx:columns
/mx:DataGrid

At this point my data in e.target.lastResult.channel.item is not always
sorted properly.  I'd like to sort it by pubDate descending before filling
out the DataGrid.  Any guidance on this?





--
Flexcoders Mailing List
FAQ: http://groups.
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Alternative FAQ location: https://share.
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e6
2079f6847
acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62079f6847
Search Archives: http://www.mail-
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo
archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links


   (Yahoo! ID required)

   mailto:flexcoders-fullfeat
mailto:flexcoders-fullfeatu