【问题标题】:How to sort an ArrayCollection in Flex如何在 Flex 中对 ArrayCollection 进行排序
【发布时间】:2012-03-14 19:23:52
【问题描述】:

我想按 fieldName 对 Arraycollection 进行升序排序。这是我的代码,我想知道它是否正确。你有什么建议吗?

public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void 
    {var dataSortField:SortField = new SortField();
        dataSortField.name = fieldName;
        dataSortField.numeric = isNumeric;
        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];
        arrCol.sort = numericDataSort;
        arrCol.refresh();}

【问题讨论】:

    标签: actionscript-3 apache-flex flex3 static-data


    【解决方案1】:

    您的代码是正确的,但类型除外。 arrCol 应该是 ar。该代码看起来几乎与博客Flex Examples 中的代码一模一样,这也是正确的。

    只需更改就是将arrCol 更改为ar,如下所示:

    public static function arrayCollectionSort(ar:ArrayCollection, fieldName:String, isNumeric:Boolean):void 
    {
        var dataSortField:SortField = new SortField();
        dataSortField.name = fieldName;
        dataSortField.numeric = isNumeric;
        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];
        ar.sort = numericDataSort;
        ar.refresh();
    }
    

    不确定数字,但其他一切都正确。

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        您的代码很好,尽管如此,这里有几个示例,其中对按钮单击应用了数字和字母排序。

        字母排序是对 2 个属性进行排序的一个很好的例子。在这种情况下,主要排序在“名字”上完成,次要排序在“姓氏”上完成。

        数字排序是相当灵活的,如果你为排序字段的数字参数提供一个布尔值true,排序会将属性转换为数字并按数字排序。如果您提供布尔值 false,则使用内置字符串比较函数。在比较之前,每个数据项都被转换为 String() 函数。使用默认值 null 时,对第一个数据项进行自省以查看它是数字还是字符串,并根据该自省进行排序。

        <?xml version="1.0" encoding="utf-8"?>
        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">
        
            <mx:Button label="Sort by first then last name" click="sortItemsByName()"/>
            <mx:Button label="Sort by number" click="sortItemsByNumber()"/>
        
            <mx:DataGrid dataProvider="{items}"
                         width="300"
                         height="300">
                <mx:columns>
                    <mx:DataGridColumn dataField="number"/>
                    <mx:DataGridColumn dataField="firstname"/>
                    <mx:DataGridColumn dataField="lastname"/>
                </mx:columns>
            </mx:DataGrid>
        
            <mx:ArrayCollection id="items">
                <mx:Object number="3" firstname="John" lastname="Brown"/>
                <mx:Object number="1" firstname="Kate" lastname="Brown"/>
                <mx:Object number="4" firstname="Jeremy" lastname="Ryan"/>
                <mx:Object number="5" firstname="Joe" lastname="Wilson"/>
                <mx:Object number="2" firstname="Greg" lastname="Walling"/>
            </mx:ArrayCollection>
        
            <mx:Script>
                <![CDATA[           
                    import mx.collections.ArrayCollection;
                    import mx.collections.Sort;
                    import mx.collections.SortField;
        
                    /**
                     * Sort the arraycollection by the firstname and then the last name
                     * */
                    private function sortItemsByName():void{
                        var srt:Sort = new Sort();
                        srt.fields = [new SortField("firstname"), new SortField("lastname")];
                        items.sort = srt;
                        items.refresh();
                    }
        
                    /**
                     * Sort the arraycollection numerically
                     * */
                    private function sortItemsByNumber():void{
                        var srt:Sort = new Sort();
                        srt.fields = [new SortField("number", true, false, true)];
                        items.sort = srt;
                        items.refresh();
                    }
        
                ]]>
            </mx:Script>
        </mx:Application>
        

        这里还有 sortField 的语言参考...

        http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/SortField.html

        ...以及数据提供者和集合的 Adob​​e livedocs 参考...

        http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_2.html

        ...这是一个很好的 livedocs 参考,用于排序和过滤...

        http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_4.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 2016-01-13
          • 1970-01-01
          • 1970-01-01
          • 2011-12-17
          相关资源
          最近更新 更多