使用Flex 3.2,我有一个扩展TitleWindow的对象。在这个TitleWindow中,我有一个AdvancedDataGrid。
在创建这个对象时,我传递了2个列表,一个要显示的数据,另一个传递了一个列定义数组。这允许我在运行时创建正确的列并为每列定义正确的显示属性(例如,布尔值将显示为复选框,日期以特定格式格式化(来自各种
问题在于,基于字符串的默认排序在100%的时间内都不起作用,即使格式排序很好,它也会在涉及空值时失败。
使用sortCompareFunction可以很容易地解决上述问题,但事实是当您设置列的sortCompareFunction时,它似乎要求您将列名硬编码到函数中。
有没有解决这个问题的方法?
供参考,这是我用来创建列的函数:
private function setupGrid():void
{
var localcols:Array = new Array();
datagrid1.columns = new Array();
var gcol:AdvancedDataGridColumn = new AdvancedDataGridColumn();
gcol.headerText = "Group";
gcol.dataField="GroupLabel";
localcols.push(gcol);
for each (var s:Object in DGColumns)
{
var col:AdvancedDataGridColumn = new AdvancedDataGridColumn();
col.headerText = s.Header;
col.dataField=s.Column;
col.headerWordWrap = true;
//col.sortable = false;
try
{
switch (s.type)
{
case "boolean":
col.labelFunction = boolCellLabel;
break;
case "date":
col.labelFunction = dateCellLabel;
col.sortCompareFunction = dateCompare; // doesn't work, as the function requires the column name coded into it
// inline does not work because s.Column is still set to the last column in the grid at sort time
col.sortCompareFunction = function (obj1, obj2):int
{
var l:XML = obj1 as XML;
var r:XML = obj2 as XML;
var left:Date = new Date( l.elements(s.Column).valueOf() );
var right:Date = new Date( r.elements(s.Column).valueOf() );
if (left == null && right == null) return 0;
else if (left == null) return -1
else if (right == null) return 1
else if (left < right) return -1;
else if (left > right) return 1;
else return 0;
;
break;
case "string": // use defaults
default:
break;
}
}
catch (ex:Object)
{}
localcols.push(col);
}
datagrid1.columns = localcols;
}
和列定义:
var DGColumns:Array = [{Header:"Lorem ipsum", Column: "lorem", type:"string"},
{Header:"dolor", Column: "dolor" , type:"string"},
{Header:"sit amet", Column: "sitAmet", type:"string"},
{Header:"consectetur", Column: "consecteturDate", type:"date"},
{Header:"adipiscing", Column: "adipiscingDate", type:"date"},
{Header:"elit", Column: "isElit" , type:"boolean"}
]
答案 0 :(得分:2)
找到答案。这是一个多部分的解决方案。
第1部分:首先设置比较功能:
col.sortCompareFunction = dateCompare;
第2部分:将一个headerRelease事件处理程序添加到AdvancedDataGrid
<mx:AdvancedDataGrid -otherAttributes- headerRelease="HeaderClick(event)" >
第3部分:定义事件处理程序和全局变量以记住列
private var clickedCol:AdvancedDataGridColumn
private function HeaderClick(event:AdvancedDataGridEvent):void
{
clickedCol = AdvancedDataGridColumn(event.currentTarget.columns[event.columnIndex]);
}
第4部分:告诉比较函数如何使用列
public function dateCompare(obj1:Object, obj2:Object):int
{
trace ("compare");
var l:XML = obj1 as XML;
var r:XML = obj2 as XML;
var lDateStr:String = l.elements(clickedCol.dataField).valueOf()
var rDateStr:String = r.elements(clickedCol.dataField).valueOf()
// compare logic goes here
}
第5部分:构建/运行/利润
答案 1 :(得分:0)
声明所有排序比较函数(类级别),并在运行时将sortCompareFunction的名称分配给sortCompareFunction。
答案 2 :(得分:0)
protected function headerReleaseHandler(event:AdvancedDataGridEvent):void
{
//event.dataField will give you column name
//call sort compare fn
}