一次展開Tree元件的所有節點(Expand All Children of Tree)

展開一個Tree元件的所有節點的方法,有兩個階段:
1. 走訪所有節點
2. 如果是枝幹(branch)結點,就打開它( 有預設的方法可以使用, expandChildrenOf( node, open) )

要提醒的地方是,所有ListBase類型的元件,裡頭都有一個collectionChangeHandler的涵式,我們可以複寫他,讓每次資料重讀之後,就打開全部節點。
請見下方原始碼 :)

===
All ListBase-components handle collection with a collectionChangeHandler(event). All we have to do is override it by adding a expandAll() function. How will we do with this expandAll()? See following sample:

override protected function collectionChangeHandler(event:Event):void
{
    super.collectionChangeHandler( event );
   
    if( expandChildrenAfterReset )
    {
        if( CollectionEvent( event ).kind == CollectionEventKind.RESET )
        {
            expandAll();
        }
    }
}


public function expandAll():void
{
    if( collection )
    {
        var c:IViewCursor = collection.createCursor();
        var o:Object;
        while( !c.afterLast )
        {  
            o = c.current;
           
            if( dataDescriptor.isBranch( o ) )
                expandChildrenOf( o ,true );

            c.moveNext();
        }
    }
}

As you see, we just iterate all tree node, determining if it is a branch and then call the built-in expandChildreOf(node,open) method.

Tags: ,

Leave a Reply