flex Tree xmlNode jsonData,更换treeData

  这里呈现出源码,查看一下结果。看一下json格式数据和xml格式数据的使用,还有就是更换数据后树的数据显示正常。
引用

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
initialize="initTree()"
>
    <mx:Script>
        <![CDATA[
        import mx.controls.Alert;
            [Bindable]
            public var open:Object = new Object();
            [Bindable]

            public var refreshData:Boolean = false;
            [Bindable]
            public var switchObj:Object = new Object();
            [Bindable]

            public var firstObj:Object = new Object();
            [Bindable]

            public var firstObj1:Object = new Object();
            [Bindable]

            public var firstObj2:Object = new Object();
            [Bindable]

            public var provider:String = "firstObj";

            private function initTree():void

            {

                firstObj = new Object();
                firstObj.label = "Foods";
                firstObj.children = new Array();
                firstObj1.label = "Fruits";
                firstObj1.children = new Array();
                firstObj2.label = "Oranges";
                firstObj1.children[0] = firstObj2;
                firstObj.children[0] = firstObj1;
                switchObj = [
                {label:'root1',children:[{label:"firstChilden"},{label:"secondChild"}]}
                ,
                {label:'root2',children:[{label:"firstChilden"},{label:"secondChild"}]}
                ];
            }

            public function changeProvider():void
            {
                open = SampleTree.openItems;
                refreshData = true;
                if (provider == "firstObj")

                {
                    SampleTree.labelField="label";
                    provider = "switchObj";
                    SampleTree.dataProvider = switchObj;
                }
                else if (provider == "switchObj")
                {
                    provider = "xmlObj";
                    SampleTree.labelField="@label";
                    SampleTree.dataProvider = treeData;
                }
                 else if (provider == "xmlObj")
                {
                    provider = "firstObj";
                    SampleTree.labelField="label";
                    SampleTree.dataProvider = firstObj;
                }
            }

            public function renderTree():void{
                if(refreshData){

                    // Refresh all rows on next update.
                    SampleTree.invalidateList();
                    refreshData = false;
                   SampleTree.openItems = open;
                    // Validate and update the properties and layout
                    // of this object and redraw it, if necessary.
                    SampleTree.validateNow();
                }

            }
        ]]>
    </mx:Script>
        <mx:XMLList id="treeData">
    <node label="Mail Box">
        <node label="Inbox">
            <node label="Marketing"/>
            <node label="Product Management"/>
            <node label="Personal"/>
        </node>
        <node label="Outbox">
            <node label="Professional"/>
            <node label="Personal"/>
        </node>
        <node label="Spam"/>
        <node label="Sent"/>
</node>
    </mx:XMLList>
    <mx:Tree id="SampleTree" render="renderTree()"  showRoot="true"  width="250" dataProvider="{firstObj}" labelField="label" />

    <mx:Button label="Change Data Provider" click="changeProvider()"/>
</mx:Application>


你可能感兴趣的:(xml,json,Flex)