Add children to HierarchicalCollectionView

南楼画角 提交于 2019-12-24 13:45:49

问题


I'm trying to implement lazy loading on ADG. and I'm stuck in the point where I need to add the returned children to the expanded node.

Currently I'm doing:

private function childrenloaded(data:*,item:*):void
{
    var len:int = data.length;
    for(var i=0;i<len;i++)
        (internalMainGrid.dataProvider as HierarchicalCollectionView).addChild(item, data[i]);
}

This works fine, but it is slow. (21,990 ms for total: 919 records) is there some way I can speed this up? - like setting children directly Ex:

var d:LazyHierarchicalData = (internalMainGrid.dataProvider as HierarchicalCollectionView).source as LazyHierarchicalData;
var ind:int = (d.source as RemoteCursorArray).getItemIndex(item);
(d.source as RemoteCursorArray)[ind].children = data;
(d.source as RemoteCursorArray).dispatchEvent(new CollectionEvent(CollectionEvent.COLLECTION_CHANGE));

But this is not refreshing the view.

Any help or direction towards a better solution is welcome. Thanks in advance!


回答1:


I've done the same thing. From what I've found - using a GroupingCollection(2) and/or SummaryRow's really slows it down. Not to mention the entire tree open-close is reset when fetching new data at a particular node.

From smoke-test performance, it would take approx 5sec to pull/paint ~2000 rows. It now takes about 1/2sec or less for 100-200 rows at each node (not counting the initial paint lag when first calling the ADG to the screen).

For me, I have done the following:

  1. Extend HierarchicalData to customize when children nodes will exist based on summary data I fetch every time from the db, and to specify which property contains the children to be displayed (if different from the children property) - I do this because of the Models I use in a RobotLegs MVC platform.
  2. Extend the AdvancedDataGridGroupItemRenderer to visually change the color of the disclosure icon (triangle graphic) - to let the user know which elements need to be fetched vs. a node that already has data. Assign this to the groupItemRenderer property.
  3. Use AdvancedDataGridEvent.ITEM_OPEN listener to dispatch an event which contains the event.item property so that a command > service > responder updates the the model at the exact node requested by the UI (adding to the children property).
  4. Add the appropriate CollectionChangeEvent to the top-level collection (it's children are ArrayCollections - nested collections are not binding)
  5. Call the dataprovider refresh method after the responder has declared it's complete.

Pseudo Examples below:

//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;
    }
}



回答2:


You should dispatch a collection_change event from internalMainGrid.dataProvider to fire a change in the view or invoke a refresh() method (it belongs to ICollectionView which is implemented by HierarchicalCollectionView as an implementor of IHierarchicalCollectionView) on the same dataProvider.



来源:https://stackoverflow.com/questions/12498719/add-children-to-hierarchicalcollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!