Chen Yangjian's Blog

Carpe diem - Seize the day

Flex DataGrid Column 自动排序

| Comments

应用 Flex 开发,碰到的很常见的需求是对 DataGrid 的数据排序。今天碰上的是,DataGrid 生成之后自动按照数据单元的名称对其进行排序。

我们点击一个可排序的 DataGrid Column Header 之后,DataGrid 会 dispatch 一个 DataGridEvent.HEADER_RELEASE 事件。DataGrid 自己同时也监听这个事件,收到后就会对其进行排序。如果 SortFunction 木有制定,则按照数据类型的默认排序进行排序。所以,要让 DataGrid 自动排序,我们只需要人肉 dispatch 一个 HEADER_RELEASE 事件就行。

值得注意的是,这个事件是由 DataGrid 发出的,不是 DataGridColumn。代码如下:

grid.dispatchEvent( new DataGridEvent(DataGridEvent.HEADER_RELEASE, false, true, 1, null, 0, null, null, 0) );

参数

DataGridEvent(DataGridEvent.HEADER_RELEASE, bubbles, cancelable, columnIndex, currentTarget, dataField, itemRenderer, reason, rowIndex, target);

columnIndex 从 0 开始。 ref

题外话:ActionScript 的框架很成熟,让我觉得不爽的地方只有两个,源码的缩进风格、仍然有大量改进空间的编译器。当初 Adobe 结合 Google 宣布 Flash 将在 Chrome 中默认安装,对所谓 Flash Killer 的 HTML5 冲击其实不小的。意思就是你看,爷还会喘气儿。

Comments