2011-09-08 41 views
6

上的默认排序我有一个火花DataGrid组件与几列,我想我的应用程序默认降序排列在DataGrid的第一列。我想使用单击顶部标题时出现的内置默认排序。我不需要对正在处理的ArrayCollection进行排序或更改比较器的内容。设置Spark DataGrid列在应用程序的creationComplete(Flex 4.5)

我也想要任何用户生成的排序,如点击不同的列标题来覆盖默认排序。

有没有人有关于如何去做这件事的任何想法?谢谢。

+0

对于MX DataGrid,我将通过对ArrayCollection应用默认排序来提供默认排序;然后可以通过单击标题更改该使用。我会假设Spark Grid将支持相同的方法。 – JeffryHouser

回答

10

只需使用sortByColumn方法:

var columnIndexes:Vector.<int> = Vector.<int>([ 0 ]); 
dataGrid.sortByColumns(columnIndexes, true); 

这是一个完整的例子:

DataGridSort.mxml

<?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" 
    creationComplete="sortDataGrid();"> 

    <fx:Script> 
    <![CDATA[ 
     import mx.collections.ArrayCollection; 
     import mx.collections.ArrayList; 

     [Bindable] 
     private var dataProvider:ArrayCollection = new ArrayCollection(
     [ 
      new Product("iPad", "Detroit", 599), 
      new Product("iPod", "Burbank", 49), 
      new Product("iPod Nano", "Burbank", 39), 
      new Product("Flash Drive", "Burbank", 59), 
      new Product("iPod", "Burbank", 49), 
      new Product("Galaxy Tab", "Coldbridge", 499), 
      new Product("HTC Hero", "Abidjan", 700) 
     ]); 

     private function sortDataGrid():void 
     { 
      // you can also use Vector.<int>([ 0, 1 ]); to sort by first 2 columns 
      var columnIndexes:Vector.<int> = Vector.<int>([ 0 ]); 

      // set 2nd argument to true to show sorting triangle 
      dataGrid.sortByColumns(columnIndexes, true); 
     } 

    ]]> 
    </fx:Script> 

    <s:DataGrid id="dataGrid" horizontalCenter="0" verticalCenter="0" width="200" 
     dataProvider="{dataProvider}"> 
     <s:columns> 
      <s:ArrayCollection> 
       <s:GridColumn dataField="name"/> 
       <s:GridColumn dataField="location"/> 
       <s:GridColumn dataField="price"/> 
      </s:ArrayCollection> 
     </s:columns> 
    </s:DataGrid> 

</s:Application> 

Product.as

package 
{ 
import flash.events.EventDispatcher; 

public class Product extends EventDispatcher 
{ 

    public function Product(name:String = null, location:String = null, price:Number = 0) 
    { 
     super(); 

     this.name = name; 
     this.location = location; 
     this.price = price; 
    } 

    public var name:String; 

    public var location:String; 

    public var price:Number; 

} 
} 
+0

谢谢!这正是我所期待的。 – buddyp450

+0

你对'中的默认排序有什么体验吗? - 我需要这个组件来启用分组 – powerMicha

0

或者,如果您不希望数据网格实际进行排序,例如,当您使用已排序的项目处理分页系统时。扩展spark数据网格并添加以下方法:

public function PlaceSortIndicator(columnIndex:uint, descending:Boolean):void 
{ 
    if(columnIndex >= 0 && columns != null && columns.length > columnIndex) 
    { 
     var column:GridColumn = columns.getItemAt(columnIndex) as GridColumn; 
     if(column != null) 
     { 
      column.sortDescending = descending; 
      if (columnHeaderGroup) 
       columnHeaderGroup.visibleSortIndicatorIndices = new <int>[columnIndex]; 
     } 
    } 
} 
相关问题