0

我想根据保存在数据库中的商店制作一棵树。我正在从数据库中获取所有商店,并通过操作脚本为每个商店制作树,并设置其数据提供者。

我的代码是:
var customTree:Tree = new Tree();
customTree.showRoot = false;
customTree.labelField = '标签';
customTree.dataProvider = xmlval; // xmlval 是我的 XML

当我运行应用程序时,它会按原样打印 xml。但是,当我将树控件设为静态并仅通过操作脚本设置其数据提供者时,它就可以正常工作。
请帮我。

4

1 回答 1

0

看看这段代码。我认为你应该使用“@label”作为 labelField 而不仅仅是“label”

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600" creationComplete="init()">
<fx:Script>
    <![CDATA[
        import mx.controls.Tree;
        [Bindable]private var xml:XML = new XML(
            <item label="root">
                <item label="Section A">
                  <item label="Section A.1"/>
                  <item label="Section A.2"/>
                  <item label="Section A.3"/>
                </item>
                <item label="Section B">
                  <item label="Section B.1"/>
                  <item label="Section B.2"/>
                  <item label="Section B.3"/>
                </item>
            </item>
        );

        private function init():void
        {
            var customTree1:Tree = new Tree();

            customTree1.dataProvider = xml;
            customTree1.labelField = "label";
            customTree1.width = 200;
            customTree1.height = 300;

            vgMain1.addElement(customTree1);


            var customTree2:Tree = new Tree();

            customTree2.dataProvider = xml;
            customTree2.labelField = "@label";
            customTree2.width = 200;
            customTree2.height = 300;

            vgMain2.addElement(customTree2);

        }
    ]]>
</fx:Script>

<s:HGroup top="20" left="20" width="600" height="300">

    <s:VGroup width="200" height="100%">
        <s:Label text="MXML Tree"/>
        <mx:Tree dataProvider="{xml}" labelField="@label" width="100%" height="300"/>
    </s:VGroup>

    <s:VGroup id="vgMain1" width="200" height="100%">
        <s:Label text="AS Tree with 'label'"/>
    </s:VGroup>

    <s:VGroup id="vgMain2" width="200" height="100%">
        <s:Label text="AS Tree with '@label'"/>
    </s:VGroup>

</s:HGroup> 
</s:Application>
于 2013-06-12T09:43:52.423 回答