flex datagrid in numerical order, alphabetical, date sort order

flex only support the default sort order string and numer two, read data from the database to the flex front-end, very often could have been converted to Strng Number type type, so order into chaos. One way to write himself a sort of clicking on the column head when the reset button on this column sort function. In addition this method also supports the date format, current is limited.

Usage: sort of form you need with headerRelease event.

<? Xml version = "1.0" encoding = "utf-8"?>
<Mx: Application name = "DataGridColumn_sortDescending_test"
xmlns: MX = " http://www.adobe.com/2006/mxml "
layout = "vertical"
verticalAlign = "middle"
backgroundColor = "white">
<mx:Script source="org/util/SortUtil.as"/>
<mx:Script>
<! [CDATA [
import mx.controls.DateField;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.events.AdvancedDataGridEvent;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.DataGridEvent;

[Bindable]
public var ac: ArrayCollection = new ArrayCollection ([
{Data: "-1112", label: "ad", date: "2009/11/10"},
{Data: "112", label: "a", date: "2009/11/11"},
{Data: "1", label: "12b", date: "2009/11/01"},
{Data: "2", label: "Ass", date: "2009/11/20"},
{Data: "0", label: "ass", date: "2009/11/14"},
{Data: "3", label: "csx", date: "2010/11/12"},
{Data: "1232", label: "cdf", date: "2009/11/13"},
{Data: "4", label: "xzm", date: "2001/12/10"},
{Data: "345", label: "awq", date: "2009/01/10"}
]);

]]>
</ Mx: Script>
<Mx: DataGrid
rowCount = "10" dataProvider = "{ac}"
headerRelease = "dataGridSort (event)" width = "600">
<mx:columns>
<mx:DataGridColumn dataField="label" headerText=" String " />
<mx:DataGridColumn dataField="data" headerText=" Digital "/>
<mx:DataGridColumn dataField="date" headerText=" Date "/>
</ Mx: columns>
</ Mx: DataGrid>
<mx:AdvancedDataGrid dataProvider="{ac}" headerRelease="advancedDataGridSort(event)" width="600" rowCount="10">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="label" headerText=" String " />
<mx:AdvancedDataGridColumn dataField="data" headerText=" Digital " />
<mx:AdvancedDataGridColumn dataField="date" headerText=" Date " />
</ Mx: columns>
</ Mx: AdvancedDataGrid>
</ Mx: Application>
<? Xml version = "1.0" encoding = "utf-8"?>
<Mx: Application name = "DataGridColumn_sortDescending_test"
xmlns: MX = " http://www.adobe.com/2006/mxml "
layout = "vertical"
verticalAlign = "middle"
backgroundColor = "white">
<mx:Script source="org/util/SortUtil.as"/>
<mx:Script>
<! [CDATA [
import mx.controls.DateField;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.events.AdvancedDataGridEvent;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.DataGridEvent;

[Bindable]
public var ac: ArrayCollection = new ArrayCollection ([
{Data: "-1112", label: "ad", date: "2009/11/10"},
{Data: "112", label: "a", date: "2009/11/11"},
{Data: "1", label: "12b", date: "2009/11/01"},
{Data: "2", label: "Ass", date: "2009/11/20"},
{Data: "0", label: "ass", date: "2009/11/14"},
{Data: "3", label: "csx", date: "2010/11/12"},
{Data: "1232", label: "cdf", date: "2009/11/13"},
{Data: "4", label: "xzm", date: "2001/12/10"},
{Data: "345", label: "awq", date: "2009/01/10"}
]);

]]>
</ Mx: Script>
<Mx: DataGrid
rowCount = "10" dataProvider = "{ac}"
headerRelease = "dataGridSort (event)" width = "600">
<mx:columns>
<mx:DataGridColumn dataField="label" headerText=" String " />
<mx:DataGridColumn dataField="data" headerText=" Digital "/>
<mx:DataGridColumn dataField="date" headerText=" Date "/>
</ Mx: columns>
</ Mx: DataGrid>
<mx:AdvancedDataGrid dataProvider="{ac}" headerRelease="advancedDataGridSort(event)" width="600" rowCount="10">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="label" headerText=" String " />
<mx:AdvancedDataGridColumn dataField="data" headerText=" Digital " />
<mx:AdvancedDataGridColumn dataField="date" headerText=" Date " />
</ Mx: columns>
</ Mx: AdvancedDataGrid>
</ Mx: Application>

import mx.controls.DateField;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.AdvancedDataGridEvent;
import mx.events.DataGridEvent;
import mx.utils.ObjectUtil;
/ **
* Click the button to sort his head out dataGrid
* /
public function dataGridSort (evt: DataGridEvent): void {
/ / 1 to get the column to sort
var column: DataGridColumn = DataGridColumn (evt.currentTarget.columns [evt.columnIndex]);
/ / 2 by column name
var columnName: String = column.dataField;
/ / 3 override the sort function of this column
column.sortCompareFunction = function (obj1: Object, obj2: Object): int {
return superSort (obj1, obj2, columnName);
};
}
/ **
* Click on AdvancedDataGrid column sort button head
* /
private function advancedDataGridSort (evt: AdvancedDataGridEvent): void {
var column: AdvancedDataGridColumn = AdvancedDataGridColumn (evt.currentTarget.columns [evt.columnIndex]);
var columnName: String = column.dataField;
column.sortCompareFunction = function (obj1: Object, obj2: Object): int {
return superSort (obj1, obj2, columnName);
};
}
/ **
* According to different data types to select a different priority is to sort numbers, dates, strings
* /
public function superSort (obj1: Object, obj2: Object, columnName: String): int {
/ / Verify the data type Numbe Date String
if (! isNaN (obj1 [columnName])) {
return number_sortCompareFunc (obj1, obj2, columnName);
}
if (strIsDate (obj1 [columnName])) {
var inputFormat: String = getInputFormat (obj1 [columnName]);
return date_sortCompareFunc (obj1, obj2, columnName, inputFormat);
}
return string_sortCompareFunc (obj1, obj2, columnName);
}
/ **
* Date sorting
* /
private function date_sortCompareFunc (obj1: Object, obj2: Object, columnName: String, inputFormat: String): int {
var date1: Date = DateField.stringToDate (obj1 [columnName], inputFormat);
var date2: Date = DateField.stringToDate (obj2 [columnName], inputFormat);
return ObjectUtil.dateCompare (date1, date2);
}
/ **
* Sort strings
* /
private function string_sortCompareFunc (obj1: Object, obj2: Object, columnName: String): int {
var str1: String = obj1 [columnName];
var str2: String = obj2 [columnName];
return ObjectUtil.stringCompare (str1, str2, false);
}
/ **
* Number of sort
* /
private function number_sortCompareFunc (obj1: Object, obj2: Object, columnName: String): int {
var num1: Number = Number (obj1 [columnName]);
var num2: Number = Number (obj2 [columnName]);
return ObjectUtil.numericCompare (num1, num2);
}
/ **
* Verify whether the date format string
* /
private function strIsDate (str: String): Boolean {
var inputFormat: String = getInputFormat (str);
/ / If you can not find the date that a direct return false symbol
if (inputFormat == null) {
return false;
}
var date: Date = DateField.stringToDate (str, inputFormat);
/ / If the date is a date not null
if (date! = null) {
return true;
} Else {
return false;
}
}
/ **
* Get the date format to determine the order of 2009/11/12 2009.11.12 2009-11-12
* /
private function getInputFormat (str: String): String {
if (str.indexOf ("-")!=- 1) {
return "YYYY-MM-DD";
}
if (str.indexOf ("/")!=- 1) {
return "YYYY / MM / DD";
}
if (str.indexOf (".")!=- 1) {
return "YYYY.MM.DD";
}
return null;
.}
/ / ActionScript file Han Jing Novo dataGrid sort Remarks: flex is the soul of the event
import mx.controls.DateField;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.AdvancedDataGridEvent;
import mx.events.DataGridEvent;
import mx.utils.ObjectUtil;
/ **
* Click the button to sort his head out dataGrid
* /
public function dataGridSort (evt: DataGridEvent): void {
/ / 1 to get the column to sort
var column: DataGridColumn = DataGridColumn (evt.currentTarget.columns [evt.columnIndex]);
/ / 2 by column name
var columnName: String = column.dataField;
/ / 3 override the sort function of this column
column.sortCompareFunction = function (obj1: Object, obj2: Object): int {
return superSort (obj1, obj2, columnName);
};
}
/ **
* Click on AdvancedDataGrid column sort button head
* /
private function advancedDataGridSort (evt: AdvancedDataGridEvent): void {
var column: AdvancedDataGridColumn = AdvancedDataGridColumn (evt.currentTarget.columns [evt.columnIndex]);
var columnName: String = column.dataField;
column.sortCompareFunction = function (obj1: Object, obj2: Object): int {
return superSort (obj1, obj2, columnName);
};
}
/ **
* According to different data types to select a different priority is to sort numbers, dates, strings
* /
public function superSort (obj1: Object, obj2: Object, columnName: String): int {
/ / Verify the data type Numbe Date String
if (! isNaN (obj1 [columnName])) {
return number_sortCompareFunc (obj1, obj2, columnName);
}
if (strIsDate (obj1 [columnName])) {
var inputFormat: String = getInputFormat (obj1 [columnName]);
return date_sortCompareFunc (obj1, obj2, columnName, inputFormat);
}
return string_sortCompareFunc (obj1, obj2, columnName);
}
/ **
* Date sorting
* /
private function date_sortCompareFunc (obj1: Object, obj2: Object, columnName: String, inputFormat: String): int {
var date1: Date = DateField.stringToDate (obj1 [columnName], inputFormat);
var date2: Date = DateField.stringToDate (obj2 [columnName], inputFormat);
return ObjectUtil.dateCompare (date1, date2);
}
/ **
* Sort strings
* /
private function string_sortCompareFunc (obj1: Object, obj2: Object, columnName: String): int {
var str1: String = obj1 [columnName];
var str2: String = obj2 [columnName];
return ObjectUtil.stringCompare (str1, str2, false);
}
/ **
* Number of sort
* /
private function number_sortCompareFunc (obj1: Object, obj2: Object, columnName: String): int {
var num1: Number = Number (obj1 [columnName]);
var num2: Number = Number (obj2 [columnName]);
return ObjectUtil.numericCompare (num1, num2);
}
/ **
* Verify whether the date format string
* /
private function strIsDate (str: String): Boolean {
var inputFormat: String = getInputFormat (str);
/ / If you can not find the date that a direct return false symbol
if (inputFormat == null) {
return false;
}
var date: Date = DateField.stringToDate (str, inputFormat);
/ / If the date is a date not null
if (date! = null) {
return true;
} Else {
return false;
}
}
/ **
* Get the date format to determine the order of 2009/11/12 2009.11.12 2009-11-12
* /
private function getInputFormat (str: String): String {
if (str.indexOf ("-")!=- 1) {
return "YYYY-MM-DD";
}
if (str.indexOf ("/")!=- 1) {
return "YYYY / MM / DD";
}
if (str.indexOf (".")!=- 1) {
return "YYYY.MM.DD";
}
return null;
}

分类:Flash 时间:2010-10-29 人气:172
分享到:
blog comments powered by Disqus

相关文章

  • 操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP) 2015-01-01

    不管是用 PHP 还是 C#, 在操作 XML 的时候我们除了一个节点一个节点去取值之外, 还有一个非常方便的表达式, 就是 XPATH 而昨晚在使用 XPATH 的时候, 遇到一个问题, 改了一个晚上才搞定, 差点没吐血. 果然基础知识要掌握扎实啊!! 假设有以下一份 XML 文档: 我们要获取所有歌曲的标题, 一般是使用以下的 XPATH 表达式: /playlist/trackList/track/title 但是匹配的结果会让你非常失望, 你会发现什么也得不到. 于是我在这个问题上卡了好

  • Using xml and annotation to achieve a similar spring dependency injection and automatic scanning function of class 2010-06-02

    As we all know, spring dependency injection can be achieved in two ways xml and annotation, but also provides automatic scanning function of the class, thereby greatly simplifying the development. Today nothing to do, also achieved a similar function

  • Flex DataGrid multi column sort based on how (Multiple Column Sort) function 2011-01-13

    We know that Flex 3.0 DataGrid before there can be only one default sort, can not multi-column sorting. In fact, multi-column sort order is also not difficult. Sort and SortField as long as we use these two objects can achieve this goal. But first th

  • JavaScript html js Table Sort 2009-05-04

    Process of implementation at the client are on the table to sort, has the following characteristics: 1. Custom sort out, sorting attributes (such as innerHTML), sort data types (including int, float, date, string), sort order (the order and reverse o

  • Jetty Oracle DataSource Config (Jetty Oracle data source configuration) 2010-08-18

    Jetty Oracle DataSource Config (Jetty Oracle data source configuration) Jetty Oracle DataSource Config (Jetty Oracle data source configuration) in accordance with the official explanation, there are three: 1. Jetty Oracle DataSource Config (Jetty Ora

  • Perl Language Overview 2010-02-21

    1.1 from scratch, we believe that Perl is an easy to learn and use language, and we hope to prove that we are right. Simple one aspect of Perl is that you do not need to say before you say something that a lot of other things. In many other programmi

  • php function array_filter () filter to remove empty _php empty array php array _ Array reset sequence of keys 2010-05-15

    Before judging whether the array element is empty, the general use of foreach, because the more specific application is to foreach, in fact, if a simple air filter array php, you can use the php function array_filter (). array array_filter (array [,

  • linux personal notes 2010-06-30

    1, extract tar-xvzf php-5.2.13.tar.gz 2. Compile cd php-5.2.13 . / Configure - prefix = / usr / local / php - with-config-file-path = / usr / local / php / conf - with-apxs2 = / usr / local / apache / bin / apxs Description: - Prefix = / usr / local

  • As long as you dare come in, no school will not drop xml 2010-03-29

    1 Introduction The vast majority of resources are carried over posted http://www.xml.org.cn/ Statement first, so it was said that even Daolian SHOW TIME 2, gold and equipment XML Explorer English the official version of (free) XML.ORG.CN download (re

  • A demo application based on sqlite3 in air 2010-03-22

    Before the start of the Demo, go first to the AIR to find information, as follows: First, the definition of RIA is the acronym for Rich Internet Applications, translated into Chinese as rich Internet applications. Rich Internet applications is the in

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.039 (s). 11 q(s)