我也做过同样的事情。根据我的发现 - 使用 GroupingCollection(2) 和/或 SummaryRow 确实会减慢速度。更不用说在特定节点获取新数据时会重置整个树的开闭。
从烟雾测试性能来看,拉动/绘制约 2000 行大约需要 5 秒。现在每个节点的 100-200 行大约需要 1/2 秒或更短时间(不计算第一次调用 ADG 到屏幕时的初始绘制延迟)。
对我来说,我做了以下事情:
- 扩展
HierarchicalData
以根据我每次从数据库中获取的摘要数据自定义子节点何时存在,并指定哪个属性包含要显示的子节点(如果与 children 属性不同) - 我这样做是因为我使用的模型RobotLegs MVC 平台。
- 扩展
AdvancedDataGridGroupItemRenderer
以直观地更改披露图标(三角形图形)的颜色 - 让用户知道需要获取哪些元素与已经有数据的节点。将此分配给 groupItemRenderer 属性。
- 使用
AdvancedDataGridEvent.ITEM_OPEN
侦听器调度包含 event.item 属性的事件,以便命令 > 服务 > 响应者在 UI 请求的确切节点处更新模型(添加到 children 属性)。
- 将适当的添加
CollectionChangeEvent
到顶级集合(它的孩子是 ArrayCollections - 嵌套集合不绑定)
- 在响应者声明完成后调用 dataprovider 刷新方法。
下面的伪示例:
//Listener on Grid:
protected function onLedgerOpenSummaryNode(event:AdvancedDataGridEvent):void
{
trace('LedgerMediator.onLedgerOpenSummaryNode', event.item);
if (event.item is SummaryTransaction)
{
refreshed = false;
sumTrans = event.item as SummaryTransaction;
//note - in my service responder I add the resulting db rows to the event.item
dispatch(new AccountEvent(AccountEvent.SUMMARY_DETAIL, null, account, event.item as SummaryTransaction));
}
}
//Dataprovider assignment to grid
private function get dataProvider():HierarchicalCollectionView
{
if (!groupCollection)
{
groupCollection = new HierarchicalCollectionView();
groupCollection.source = new OnDemandCollection();
}
return groupCollection;
}
//assigns the initial source to the demand collection
private function loadTransactions():void
{
var odc:OnDemandCollection = OnDemandCollection(dataProvider.source);
odc.source = account.summaryTransactions.children;
transactionChangeDetection(true);
view.callLater(deferrDataProviderUpdate);
}
//called when the service.responder dispatches it's 'complete event'.
private function deferrDataProviderUpdate():void
{
if (refreshed) return;
dataProvider.refresh();
}
//add change event listener to the top-level collection
private function transactionChangeDetection(add:Boolean):void
{
var collection:ArrayCollection;
//nested collections are not binding up the object chain, so change listeners must be added/removed to/from each collection.
for each ( var summary:SummaryTransaction in account.summaryTransactions.collection)
{
collection = summary.transactions.collection;
add ? collection.addEventListener(CollectionEvent.COLLECTION_CHANGE, onTransactionUpdate)
: collection.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onTransactionUpdate);
}
}
//block the refresh method - only interested in add,delete,update
protected function onTransactionUpdate(event:CollectionEvent):void
{
if (event.kind == CollectionEventKind.REFRESH) return;
view.callLater(deferrDataProviderUpdate);
}
public class ExtAdvancedDataGridGroupItemRenderer extends AdvancedDataGridGroupItemRenderer
{
private var defaultColor:ColorTransform;
public function ExtAdvancedDataGridGroupItemRenderer()
{
super();
}
override protected function commitProperties():void
{
super.commitProperties();
if (disclosureIcon) defaultColor = disclosureIcon.transform.colorTransform;
}
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
if (!listData || !data) return;
var adgld:AdvancedDataGridListData = AdvancedDataGridListData(listData);
var adg:AdvancedDataGrid = adgld.owner as AdvancedDataGrid;
var hcv:HierarchicalCollectionView = adg.dataProvider as HierarchicalCollectionView;
var source:IHierarchicalData = hcv.source;
var useDefaultColor:Boolean;
var visible:Boolean;
if (!data) visible = false;
else if (!source.canHaveChildren(data)) visible = false;
else if (source.hasChildren(data))
{
useDefaultColor = true;
visible = true;
}
else
{
visible = true;
var ct:ColorTransform = new ColorTransform();
ct.color = 0xBCB383;
disclosureIcon.transform.colorTransform = ct;
}
if (useDefaultColor) disclosureIcon.transform.colorTransform = defaultColor;
disclosureIcon.visible = visible;
}
}
public class OnDemandCollection extends HierarchicalData
{
public function OnDemandCollection()
{
super();
}
override public function getChildren(node:Object):Object
{
if (node is SummaryGrouping) return SummaryGrouping(node).children;
else if (node is SummaryTransaction) return SummaryTransaction(node).children;
else if (node is Transaction) return Transaction(node).children;
return {};
}
override public function canHaveChildren(node:Object):Boolean
{
if (node is SummaryGrouping)
{
return true;
}
else if (node is SummaryTransaction)
{
var sumTran:SummaryTransaction = SummaryTransaction(node);
return sumTran.numTransactions > 0;
}
else if (node is Transaction)
{
var tran:Transaction = Transaction(node);
return tran.isSplit;
}
else if (node is Split) return false;
else if (node.hasOwnProperty('children'))
{
var list:IList = node['children'] as IList;
return list.length > 0;
}
else return false;
}
}