我有一个包含两列的 AdvancedDatagrid:代码(字符串)和值(数字)。我对每一列使用相同的排序功能。我想要做的是根据 Value 列(数字数据)对两列进行排序,但是在没有可用数字的情况下,我希望对 Code 列按字母顺序进行排序。
我用一个例子简化了我面临的问题,以代表我正在尝试做的事情:
图片显示了两列,两列的排序均基于 Value 列。如果值为 NaN,我希望 Code 列值按字母顺序排序。所以一、二、三、四将保持不变,但 BADC 将是 ABCD。
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" minWidth="955" minHeight="600" initialize="initializeHandler(event)">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.utils.ObjectUtil;
[Bindable]
private var ac:ArrayCollection;
protected function initializeHandler(event:FlexEvent):void
{
ac = new ArrayCollection();
var one:NameValueObject = new NameValueObject("One", 1);
var two:NameValueObject = new NameValueObject("Two", 2);
var three:NameValueObject = new NameValueObject("Three", 3);
var four:NameValueObject = new NameValueObject("Four", 4);
var a:NameValueObject = new NameValueObject("A", NaN);
var b:NameValueObject = new NameValueObject("B", NaN);
var c:NameValueObject = new NameValueObject("C", NaN);
var d:NameValueObject = new NameValueObject("D", NaN);
ac.addItem(one);
ac.addItem(two);
ac.addItem(three);
ac.addItem(four);
ac.addItem(b);
ac.addItem(a);
ac.addItem(d);
ac.addItem(c);
}
private function numericValueSort(obj1:Object, obj2:Object):int
{
var value1:Number = (obj1 as NameValueObject).value;
var value2:Number = (obj2 as NameValueObject).value;
return ObjectUtil.numericCompare(value1, value2);
}
private function codeLabelFunction(item:Object, column:AdvancedDataGridColumn):String
{
return (item as NameValueObject).code;
}
]]>
</mx:Script>
<mx:AdvancedDataGrid width="500" height="300" dataProvider="{ac}">
<mx:columns>
<mx:AdvancedDataGridColumn id="codeColumn"
headerText="Code"
dataField="value"
labelFunction="codeLabelFunction"
sortCompareFunction="numericValueSort">
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn id="numericValueColumn"
headerText="Value"
dataField="value"
sortCompareFunction="numericValueSort">
</mx:AdvancedDataGridColumn>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Application>
NaveValueObject 类
package
{
public class NameValueObject
{
public var code:String;
public var value:Number;
public function NameValueObject(aCode:String, aNumber:Number)
{
code = aCode;
value = aNumber;
}
}
}