Mar 09
下面的例子展示了利用sortCompareFunction属性,静态的ObjectUtil.numericCompare() 和ObjectUtil.stringCompare()事件,通过设置DataGridColumn中的自定义的比较函数,来实现数字以及区分或者不区分大小写字母的排序。
下面是具体的程序代码:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://blog.flexexamples.com/2008/03/07/performing-case-insensitive-sorts-using-the-datagrid-control-in-flex/ -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.utils.ObjectUtil;
- private function index_sortCompareFunc(itemA:Object, itemB:Object):int {
- // Make sure itemA has an "index" property.
- if (!itemA.hasOwnProperty("index")) {
- itemA.index = null;
- }
- // Make sure itemB has an "index" property.
- if (!itemB.hasOwnProperty("index")) {
- itemB.index = null;
- }
- // Perform a numeric sort.
- return ObjectUtil.numericCompare(itemA.index, itemB.index);
- }
- private function value_sortCompareFunc(itemA:Object, itemB:Object):int {
- // Make sure itemA has a "value" property.
- if (!itemA.hasOwnProperty("value")) {
- itemA.value = null;
- }
- // Make sure itemB has a "value" property.
- if (!itemB.hasOwnProperty("value")) {
- itemB.value = null;
- }
- /**
- * Perform a string sort. If the checkbox is selected
- * do a case insensitive sort, otherwise, dont.
- */
- return ObjectUtil.stringCompare(itemA.value, itemB.value, checkBox.selected);
- }
- ]]>
- </mx:Script>
- <mx:ArrayCollection id="arrColl">
- <mx:source>
- <mx:Array>
- <mx:Object index="1" value="apple" />
- <mx:Object index="200" value="Bear" />
- <mx:Object index="3" value="corn" />
- <mx:Object index="40" value="Dragon" />
- <mx:Object value="eggplant" />
- <mx:Object index="5" />
- </mx:Array>
- </mx:source>
- </mx:ArrayCollection>
- <mx:ApplicationControlBar dock="true">
- <mx:CheckBox id="checkBox"
- label="case insensitive search:"
- labelPlacement="left"
- selected="true" />
- </mx:ApplicationControlBar>
- <mx:DataGrid id="dataGrid" dataProvider="{arrColl}">
- <mx:columns>
- <mx:DataGridColumn dataField="index"
- sortCompareFunction="index_sortCompareFunc" />
- <mx:DataGridColumn dataField="value"
- sortCompareFunction="value_sortCompareFunc" />
- </mx:columns>
- </mx:DataGrid>
- </mx:Application>
下面是实际运行效果:
原文作者:Peter deHaan 翻译:minidxer

March 9th, 2008 at 1:37 am
转了一圈,不小心跑到你博客上来了.我是IDCSPY上的小猪船长,呵呵…从前博客上跑来的,你在里面骂那个人白痴!
March 9th, 2008 at 11:51 am
呵呵,当时看到他炒作的太厉害了,比较反感所以才。。。
后来想想人家自己的博客,想怎么样就怎么样,其实也没什么好指责的