我有一个简单的flex dataProvider
,如下所示:
private var ac:ArrayCollection = new ArrayCollection([
{a:"a_1", b:"b_1", hour:"16:00:00"},
{a:"a_1", b:"b_2", hour:"16:30:00"},
{a:"a_2", b:"b_2", hour:"17:00:00"},
{a:"a_3", b:"b_3", hour:"15:00:00"},
{a:"a_3", b:"b_3", hour:"16:00:00"}
]);
我想使用AdvancedDataGrid
组件将这些数据分组到a
和b
字段,并按小时字段排序。例如,我想获得:
a_3
b_3
15:00:00
16:00:00
a_1
b_1
16:00:00
b_2
16:30:00
a_2
b_2
17:00:00
但是当我添加Object
之类的{a:"a_3", b:"b_3", hour:"18:00:00"}
时,我希望此订单成为此订单:
a_1
b_1
16:00:00
b_2
16:30:00
a_2
b_2
17:00:00
a_3
b_3
15:00:00
16:00:00
18:00:00
因此,一个Object
可以修改其(a_X, b_Y)
字段的AdvancedDataGrid
函数中的所有hour
对顺序。
我想我不能使用compareFunction
,因为我必须比较多行,而不仅仅是一行(在我的示例中添加Object
修改3行)。
我对AdvancedDataGrid
不是很熟悉所以任何帮助都会受到赞赏。谢谢。
PS:实际上我在GroupingCollection2
和dataProvider
字段上使用GroupingField
作为a
和b
,我不知道这是否正确方式...
答案 0 :(得分:0)
好的,我找到了解决方案。因为我的dataField hour
是插入时间,所以当我添加Item时,我确信这个项目会放在最后。我的测试代码就是这个:
<s:Button label="add" click="generateData(event)" />
<mx:AdvancedDataGrid width="500" height="800">
<mx:dataProvider>
<mx:GroupingCollection2 id="gc" source="{ac}">
<mx:grouping>
<mx:Grouping compareFunction="compare">
<mx:GroupingField name="a" />
<mx:GroupingField name="b" />
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection2>
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="a" />
<mx:AdvancedDataGridColumn dataField="b" />
<mx:AdvancedDataGridColumn dataField="hour" />
</mx:columns>
</mx:AdvancedDataGrid>
[Bindable]
private var ac:ArrayCollection = new ArrayCollection([]);
// Array of "a" property order
private var order:Array = [];
protected function generateData(event:MouseEvent):void
{
// test data
var t:Array = [
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_1", hour:null},
{a:"a_1", b:"b_2", hour:null},
{a:"a_1", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_2", hour:null},
{a:"a_2", b:"b_3", hour:null},
{a:"a_3", b:"b_3", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_3", b:"b_4", hour:null},
{a:"a_4", b:"b_5", hour:null},
{a:"a_5", b:"b_6", hour:null},
{a:"a_5", b:"b_6", hour:null},
];
// random number of insertions
var toAddNumber:int = 10 * int(1 + Math.random());
var index:int;
// "a" type added with random insertions
var aAdded:Array = [];
var o:Object;
for(var i:int=0; i<toAddNumber; i++)
{
// random item of t added
index = (Math.floor(Math.random() * (t.length-1 - 0 + 1)) + 0);
o = t[index];
// hour initialized
o.hour = getTimer();
// we insert this item in dataProvider
ac.addItem(o);
if(aAdded.indexOf(o["a"]) == -1)
{
aAdded.push(o["a"]);
}
}
// Natural sort
aAdded.sort();
// update of order tab
for(var s:String in aAdded)
{
if(order.indexOf(aAdded[s]) != -1)
{
order.splice(order.indexOf(aAdded[s]), 1);
}
order.push(aAdded[s]);
}
// we update GroupingCollection2
gc.refresh();
}
private function compare(o1:Object, o2:Object, fields:Array = null):int
{
// order in order tab...
if(order.indexOf(o1.a) > order.indexOf(o2.a))
{
return 1;
}
else if(order.indexOf(o1.a) < order.indexOf(o2.a))
{
return -1;
}else
{
// ... then natural order on "b"...
if(o1.b > o2.b)
{
return 1;
}
else if(o1.b < o2.b)
{
return -1;
}
else{
// ... then numeric order on hour field
if(o1.hour > o2.hour)
{
return 1;
}
else if(o1.hour < o2.hour)
{
return -1;
}
else{
return 0;
}
}
}
}
这样可以正常工作:)