Search code examples
apache-flexflex4

What can be used instead of tree.dataProvider in adobe flex?


I have my .mxml as below:

  <fx:Declarations>
    <fx:XMLList id="data">
        <node>
            <node label="Inbox">
                <node label="Marketing"/>
                <node label="Product Management"/>
                <node label="Personal"/>
            </node>
            <node label="Outbox">
                <node label="Professional"/>
                <node label="Private"/>
            </node>
            <node label="Spam">kushal</node>
            <node label="Sent"/>
        </node>
    </fx:XMLList>
</fx:Declarations>

<mx:VBox>
    <mx:Button label="Search by name" click="findByName()" />  

    <mx:Tree id="tree" width="500" height="500" 
             showRoot="false" dataProvider="{data}" 
             labelField="@label" />
</mx:VBox>

I'm trying to click findByName() on click of a button: which is:

private function findByName():void
    {



        var mixml:XMLList = new XMLList(data);



        var searchStr:String = "Outbox";
        //child.expandChildrenOf(myXML[0], false);




        //mixml=data;

        searchResult= mixml.node.(@label==searchStr);



        var xn:XML = searchResult[searchResultIndex];
        Alert.show("xn"+ xn);


            searchResultIndex = 0;
        if (searchResult[searchResultIndex] != undefined)


            var xmlNode:XML = searchResult[searchResultIndex];

        while (xmlNode.parent() != null) {


            Alert.show("xmlNodeBefore"+ xmlNode);
            xmlNode = xmlNode.parent();

            Alert.show("xmlNodeAfter"+ xmlNode);

            //checkpoint 1

            tree.expandItem(xmlNode, true, false);
            tree.selectedItem = searchResult[searchResultIndex];

            Alert.show(" tree.selectedItem " + tree.selectedItem );
        }
    }

If here instead of data I use tree.dataProvider then this code doesn't work, can somebody tell me why?


Solution

  • Your code does not work when you use tree.dataProvider because it's of the type XMLListCollection, which is a wrapper on top of the XMLList object. Flex internally transforms the XMLList into a XMLListCollection, because XMLListCollection has capabilities of providing change notifications via events, which XMLList does not.

    So in your code, if you use tree.dataProvider at the following line :

    var mixml:XMLList = new XMLList(tree.dataProvider);
    

    You are actually trying to type cast and convert an XMLListCollection into a XMLList, which is why it does not work.

    Interestingly, the XMLListCollection has public properties (like most collections in Flex) which can be used to access the underlying XMLList source from which it is made up of. So, to access the data from the tree, one correct way would be:

    var mixml:XMLList = new XMLList(tree.dataProvider.source);
    

    I have tried this in your code and it seems to do the same job as your above code.

    You can read more about this here :

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/XMLListCollection.html#source

    Hope this explanation works and it answers your question overall.

    Cheers.