2

我很难将 json 传递给数据网格。我收到以下错误:

TypeError: Error #1034: Type Coercion failed: cannot convert mx.collections::ArrayCollection@bc292a9 to Array.
    at Function/<anonymous>()[C:\Users\Birger\Dropbox\Rich Media Applications\P006_Project\src\FULLTEST.mxml:10]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Binding/wrapFunctionCall()[E:\dev\4.y\frameworks\projects\framework\src\mx\binding\Binding.as:395]
    at mx.binding::Binding/innerExecute()[E:\dev\4.y\frameworks\projects\framework\src\mx\binding\Binding.as:469]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Binding/wrapFunctionCall()[E:\dev\4.y\frameworks\projects\framework\src\mx\binding\Binding.as:395]
    at mx.binding::Binding/execute()[E:\dev\4.y\frameworks\projects\framework\src\mx\binding\Binding.as:333]
    at mx.binding::BindingManager$/executeBindings()[E:\dev\4.y\frameworks\projects\framework\src\mx\binding\BindingManager.as:153]
    at FULLTEST/_FULLTEST_ArrayCollection1_i()[C:\Users\Birger\Dropbox\Rich Media Applications\P006_Project\src\FULLTEST.mxml:4]
    at FULLTEST()[C:\Users\Birger\Dropbox\Rich Media Applications\P006_Project\src\FULLTEST.mxml:4]

我的代码是:

<?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" xmlns:components="components.*" initialize="getData.send();">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <mx:HTTPService id="getData" url="http://localhost/P006_Project/Query.php" 
                        useProxy="false" method="POST" resultFormat="text" result="getPHPData(event)">  
        </mx:HTTPService>
        <s:ArrayCollection id="acItems" source="{dataArray}" />
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            import mx.rpc.events.ResultEvent;

            [Bindable]private var dataArray:ArrayCollection = new ArrayCollection();

            private function initDataGrid():void
            {
                getData.send();
            }

            private function getPHPData(event:ResultEvent):void
            {
                var rawArray:Array;
                var rawData:String = String(event.result);
                rawArray = JSON.parse(rawData) as Array;
                dataArray = new ArrayCollection(rawArray);
            }


        ]]>
    </fx:Script>

    <mx:Accordion id="accItems" creationPolicy="auto">
        <s:NavigatorContent label="Frisdranken">
            <components:FULLTESTCOMP acItems="{acItems}" creationComplete="{initDataGrid()}"/>
        </s:NavigatorContent>
    </mx:Accordion>
</s:Application>

所以我试图通过将数据库中的内容转换为 JSON 来填充我的数据网格。我正在使用一个自定义组件(在这个组件中只是一个普通的数据网格)。

4

1 回答 1

1

这是问题所在:

       <s:ArrayCollection id="acItems" source="{dataArray}" />

source 需要是“Array”类型,但您将源分配给 ArrayCollection 对象。

你应该做:

             <s:ArrayCollection id="acItems" source="{dataArray.source}" />

希望这可以帮助。

于 2012-12-17T09:55:23.700 回答