| 包 | spark.collections |
| 类 | public class SortField |
| 继承 | SortField AdvancedStyleClient EventDispatcher Object |
| 实现 | ISortField |
| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
var col:ICollectionView = new ArrayCollection();
col.addItem({first:"Anders", last:"Dickerson"});
var sort:Sort = new Sort();
var sortfield:SortField = new SortField("first", true);
sortfield.setStyle("locale", "en-US");
sort.fields = [sortfield];
col.sort = sort;
也有集合中包含简单项目(如 String、Date 和 Boolean 等)的情形。在这种情况下,应直接对简单类型应用排序。在这种情况下构造排序时,只需要一个排序字段,且不应指定 name。例如:
var col:ICollectionView = new ArrayCollection();
col.addItem("California");
col.addItem("Arizona");
var sort:Sort = new Sort();
var sortfield:SortField = new SortField(null, true);
sortfield.setStyle("locale", "en-US");
sort.fields = [sortfield];
col.sort = sort;
由 SortField 类提供的默认比较可以将字符串按照特定语言进行正确排序。可以按照以下方式之一,设置类的实例的区域设置样式以选择语言:
- 在 MXML 声明中使用该类,并从包含声明的文档中继承区域设置。 示例:
<fx:Declarations>
<s:SortField id="sf" />
</fx:Declarations>
<fx:Declarations>
<s:SortField id="sf_SimplifiedChinese" locale="zh-Hans-CN" />
</fx:Declarations>
setStyle 方法,如 sf.setStyle("locale", "zh-Hans-CN")
addStyleClient() 方法来继承 UIComponent 的样式。
隐藏 MXML 语法The <s:SortField> tag has the following attributes:
<s:SortField Properties compareFunction="Internal compare function" descending="false" name="null" numeric="null" />
相关 API 元素
| 属性 | 由以下参数定义 | ||
|---|---|---|---|
| arraySortOnOptions : int [只读]
此帮助器属性由 findItem() 和 sort() 方法内部使用。 | SortField | ||
![]() | className : String [只读]
组件类的名称。 | AdvancedStyleClient | |
| compareFunction : Function
用于在对关联集合的项目进行排序时比较两个项目的函数。 | SortField | ||
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
| descending : Boolean
指定此字段是否应按降序排序。 | SortField | ||
![]() | id : String
组件的标识。 | AdvancedStyleClient | |
![]() | inheritingStyles : Object
包含该非可视样式客户端实例的可继承样式的对象。 | AdvancedStyleClient | |
![]() | moduleFactory : IFlexModuleFactory
模块工厂作为用于查找样式管理器(控制此非可视样式客户端实例的样式)的上下文使用。 | AdvancedStyleClient | |
| name : String
要排序的字段的名称。 | SortField | ||
![]() | nonInheritingStyles : Object
此组件的非继承样式链的开头内容。 | AdvancedStyleClient | |
| numeric : Object
指定当要进行排序的字段包含数值 (number/int/uint) 或数值的字符串表示形式时,比较运算符是否使用数值比较。 | SortField | ||
![]() | styleDeclaration : CSSStyleDeclaration
具有此对象声明的内嵌样式的样式声明。 | AdvancedStyleClient | |
![]() | styleManager : IStyleManager2 [只读]
返回此组件使用的 StyleManager 实例。 | AdvancedStyleClient | |
![]() | styleName : Object
该对象的样式值的源。 | AdvancedStyleClient | |
![]() | styleParent : IAdvancedStyleClient
组件的父项用于评估后代选择器。 | AdvancedStyleClient | |
| usingCustomCompareFunction : Boolean [只读]
如果此 ISortField 使用自定义比较运算符函数,则为 true。 | SortField | ||
| 方法 | 由以下参数定义 | ||
|---|---|---|---|
构造函数。 | SortField | ||
![]() | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
使用 EventDispatcher 对象注册事件侦听器对象,以使侦听器能够接收事件通知。 | EventDispatcher | |
![]() |
删除此组件实例的样式属性。 | AdvancedStyleClient | |
![]() |
将事件调度到事件流中。 | EventDispatcher | |
![]() |
为应用于该组件的类型选择器返回 CSSStyleDeclaration 对象数组,如果不存在则返回 null。 | AdvancedStyleClient | |
![]() |
获取已在此组件的样式查找链中的某个位置设置过的样式属性。 | AdvancedStyleClient | |
![]() |
如果 currentCSSState 不为 null,则返回 true。 | AdvancedStyleClient | |
![]() |
检查 EventDispatcher 对象是否为特定事件类型注册了任何侦听器。 | EventDispatcher | |
![]() |
表示对象是否已经定义了指定的属性。 | Object | |
![]() |
该类或扩展该类的类在 MXML 声明中使用时,调用 initialized 方法。 | AdvancedStyleClient | |
设置默认比较函数以执行比较时,Sort 类将根据以下三项内容之一来调用帮助器函数:是否已设置自定义比较函数、指定字段的数据类型或 numeric 属性的值。 | SortField | ||
![]() |
表示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
如果 cssState 与 currentCSSState 匹配,则返回 true。 | AdvancedStyleClient | |
![]() |
确定此实例与给定类型相同,还是属于给定类型的子类。 | AdvancedStyleClient | |
![]() |
将样式更改传播到此样式客户端实例的子项。 | AdvancedStyleClient | |
![]() |
表示指定的属性是否存在、是否可枚举。 | Object | |
![]() |
设置内部样式缓存值,以便 getStyle() 方法可以起作用。 | AdvancedStyleClient | |
![]() |
将 EffectManager 注册为每个效果事件的事件侦听器之一。 | AdvancedStyleClient | |
![]() |
从 EventDispatcher 对象中删除侦听器。 | EventDispatcher | |
反转此排序字段的条件。 | SortField | ||
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
对此组件实例设置样式属性。 | AdvancedStyleClient | |
![]() |
检测对样式属性的更改。 | AdvancedStyleClient | |
![]() |
首次初始化组件样式时,Flex 会调用 stylesInitialized() 方法。 | AdvancedStyleClient | |
![]() |
返回此对象的字符串表示形式,其格式设置遵守区域设置特定的约定。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
![]() |
检查是否用此 EventDispatcher 对象或其任何祖代为指定事件类型注册了事件侦听器。 | EventDispatcher | |
样式为常见样式,或与特定主题关联。如果为常见样式,则可以用于任何主题。如果样式与特定主题关联,则只有应用程序使用该主题时才能使用该样式。
| 样式 | 说明 | 由以下参数定义 | ||
|---|---|---|---|---|
locale | 类型: String CSS 继承: 是 语言版本: ActionScript 3.0 产品版本: Flex 4.5 运行时版本: Flash10.1, AIR 2.5 区域设置标识符,用于指定语言、区域、脚本,还可以指定其它相关的标签和键。此标识符的语法必须遵守 Unicode 技术标准 #35 定义的语法(如 en-US、de-DE、zh-Hans-CN)。 对于基于浏览器的应用程序,默认区域设置基于浏览器的语言设置。(请注意,这不是在 Javascript 中可用的浏览器 UI 语言,而是用户已在浏览器首选参数中设置的 Web 页面首选区域设置的列表。)对于 AIR 应用程序,默认 UI 区域设置基于用户的系统首选参数。 了解详细信息 | SortField | ||
arraySortOnOptions | 属性 |
compareFunction | 属性 |
compareFunction:Function| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
用于在对关联集合的项目进行排序时比较两个项目的函数。如果指定了 ISort 对象中的 compareFunction 属性,则 Flex 会忽略 ISort 的 SortField 对象的所有 compareFunction 属性。
该比较函数必须具有以下签名:
function myCompare(a:Object, b:Object):int
此函数必须返回以下值:
- -1,如果在已排序序列中
Object a应显示在Object b之前。 - 0,如果
Object a等于Object b。 - 1,如果在已排序序列中
Object a应显示在Object b之后。
默认值为可以按升序或降序执行字符串、数字或日期比较的内部比较函数。从 SortingCollator 类调用特定于区域设置(语言、地区和脚本)的比较方法,以执行字符串比较。此类使用区域设置样式来确定区域设置。请仅在需要自定义比较算法时指定自己的函数。这种情况通常仅当在显示中使用计算的字段时出现。
实现
public function get compareFunction():Function public function set compareFunction(value:Function):voiddescending | 属性 |
name | 属性 |
numeric | 属性 |
numeric:Object| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
指定当要进行排序的字段包含数值 (number/int/uint) 或数值的字符串表示形式时,比较运算符是否使用数值比较。
未提供自定义比较函数的情况下,SortField 类会使用该属性。
如果该属性为 true,则会使用内置数值比较函数。比较前每个数据项调度给 Number() 函数。
如果该属性为 false,则会使用内置字符串比较函数。比较前每个数据项调度给 String() 函数。
如果此属性为 null,则内部检查第一个数据项以查看它是否为数字或字符串,并基于内部检查结果继续进行排序。
默认值为 null。
此属性可用作数据绑定的源。修改此属性后,将调度 numericChanged 事件。
实现
public function get numeric():Object public function set numeric(value:Object):voidusingCustomCompareFunction | 属性 |
SortField | () | 构造函数 |
public function SortField(name:String = null, descending:Boolean = false, numeric:Object = null)| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
构造函数。
参数name:String (default = null) — 此字段用来进行比较的属性的名称。如果该对象为简单类型,则传递 null。
| |
descending:Boolean (default = false) — 指示比较运算符是否按降序排列项目。
| |
numeric:Object (default = null) — 指示比较运算符是否按编号而不按字母顺序比较排序项目。
|
initializeDefaultCompareFunction | () | 方法 |
public function initializeDefaultCompareFunction(obj:Object):void| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
设置默认比较函数以执行比较时,Sort 类将根据以下三项内容之一来调用帮助器函数:是否已设置自定义比较函数、指定字段的数据类型或 numeric 属性的值。如果 numeric 属性为 true,将在排序时执行数字比较。
参数
obj:Object — 包含数据的对象。如果已通过 name 属性设置了字段名称,则将使用该名称访问此对象的数据值。否则,将使用对象本身作为数据值。
|
reverse | () | 方法 |
public function reverse():void| 语言版本: | ActionScript 3.0 |
| 产品版本: | Flex 4.5 |
| 运行时版本: | Flash Player 10.1, AIR 2.5 |
反转此排序字段的条件。例如,如果该字段原来按降序排序,则现在按升序排序。
注意:修改 ISortFields 时不会自动更新 ICollectionView;而调用 refresh() 方法可更新此视图。
<?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"
minWidth="955" minHeight="600">
<!-- Sample program for spark.collections.Sort and SortField -->
<fx:Declarations>
<s:Sort id="sortbyLastName_FirstName">
<s:fields>
<s:SortField name="last"/>
<s:SortField name="first"/>
</s:fields>
</s:Sort>
<mx:ArrayCollection id="collection" sort="{sortbyLastName_FirstName}">
<mx:source>
<fx:Object first="Anders" last="Dickerson"/>
<fx:Object first="Eileen" last="Maccormick"/>
<fx:Object first="Aiden" last="MacCormick"/>
<fx:Object first="Steve" last="MacGregor"/>
</mx:source>
</mx:ArrayCollection>
</fx:Declarations>
<s:VGroup>
<s:VGroup>
<s:HGroup>
<s:Label text="Input Locale ID Name: "/>
<s:TextInput id="inputLocaleIDName"/>
<!--
Sets the locale style on the document UI component.
The SortField and Sort objects defined in the
fx:Declarations section will inherit this style.
-->
<s:Button click="{setStyle('locale', inputLocaleIDName.text);
collection.refresh()}" label="Apply"/>
</s:HGroup>
<s:Label text="Example: 'en-US', 'fr-FR', 'zh-CN', 'ar-SA'"/>
</s:VGroup>
<s:DataGrid dataProvider="{collection}" width="100%"
creationComplete="{collection.refresh()}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="last"/>
<s:GridColumn dataField="first"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:VGroup>
</s:Application>
<?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"
minWidth="955" minHeight="600"
creationComplete="initApp()">
<!-- Sample program for spark.collections.Sort and SortField -->
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.collections.*;
[Bindable]
private var collection:ArrayCollection = new ArrayCollection();
private function localeChange():void
{
// Sets the locale style on this application.
// The SortField objects will inherit this style.
setStyle('locale', inputLocaleIDName.text);
collection.refresh();
}
private function initApp() : void
{
// Add data to the collection.
collection.addItem({first:"Anders", last:"Dickerson"});
collection.addItem({first:"Steve", last:"Maccormick"});
collection.addItem({first:"Aiden", last:"MacCormick"});
collection.addItem({first:"Eileen", last:"MacGregor"});
// Create the Sort instance.
var sort:Sort = new Sort();
// Set the sort field; sort on the last name first, first name
// second.
var sortfieldLastName:SortField = new SortField("last",true);
var sortfieldFirstName:SortField = new SortField("first",true);
sort.fields = [sortfieldLastName, sortfieldFirstName];
// Add the sort field objects to this application's list of
// style clients. This will cause the sort field objects to
// inherit the locale style from this Application.
addStyleClient(sortfieldLastName);
addStyleClient(sortfieldFirstName);
// Assign the Sort object to the view.
collection.sort = sort;
// Apply the sort to the collection.
collection.refresh();
}
]]>
</fx:Script>
<s:VGroup>
<s:VGroup>
<s:HGroup>
<s:Label text="Input Locale ID Name: "/>
<s:TextInput id="inputLocaleIDName"/>
<s:Button click="localeChange()" label="Apply"/>
</s:HGroup>
<s:Label text="Example: 'en-US', 'fr-FR', 'zh-CN', 'ar-SA'"/>
</s:VGroup>
<s:DataGrid dataProvider="{collection}" width="100%"
creationComplete="{collection.refresh()}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="last"/>
<s:GridColumn dataField="first"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:VGroup>
</s:Application>
Tue Jun 12 2018, 11:04 AM Z
显示 MXML 语法