【问题标题】:Setting Spark DataGrid column's default sort on application's creationComplete (Flex 4.5)在应用程序的 creationComplete (Flex 4.5) 上设置 Spark DataGrid 列的默认排序
【发布时间】:2011-11-13 01:58:30
【问题描述】:

我有一个包含多个列的 spark DataGrid 组件,我希望我的应用程序默认为 DataGrid 中第一列的降序。我想使用一次单击顶部标题时发生的内置默认排序。我不需要对我正在使用的 ArrayCollection 进行排序或更改比较器是什么。

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

有没有人对如何解决这个问题有任何想法?谢谢。

【问题讨论】:

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

标签: sorting apache-flex datagrid adobe


【解决方案1】:

只需使用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;

}
}

【讨论】:

  • 谢谢!这正是我想要的。
  • 您对&lt;mx:AdvancedDataGrid&gt; 中的默认排序有任何经验吗?我需要这个组件来启用分组
【解决方案2】:

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

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];
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 2011-08-11
    • 2016-09-18
    • 2013-09-03
    • 1970-01-01
    • 2013-05-21
    • 2013-12-14
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多