2

I want to display the values coming from database in a datagrid using flex. Here is my code. I am using webservice. I have database values from application1_initializeHandler() method. How to fetch these values into onResult() method and perform databinding? I want code for onResult() function and data binding. Please help..

<?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" initialize="application1_initializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.rpc.AsyncResponder;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;


            protected function application1_initializeHandler(event:FlexEvent):void
            {
                AreasOfWestBengal.loadWSDL();
                var s:String = "SELECT * FROM [CSFTestNew].[dbo].[AreasOfWestBengal]";
                var t:AsyncToken = AreasOfWestBengal.GetRec("[AreasOfWestBengal]", s, "1", "SQLExpress");
                t.addResponder(new AsyncResponder(onResult, onFault, t));
            }

            protected function onResult(event:ResultEvent, token:Object=null):void
            {


            }

            protected function onFault(event:FaultEvent, token:Object=null):void
            {
                trace(event.fault.toString());
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:WebService id="AreasOfWestBengal" wsdl="https://www.geoviewer8.com/gv8webservices/CSF_NewGVOConfig/GVOConfig.asmx?wsdl"/>
    </fx:Declarations>
    <mx:DataGrid x="197" y="83" width="348" height="216">
        <mx:columns>
            <mx:DataGridColumn headerText="Areas" dataField="Areas"/>
            <mx:DataGridColumn headerText="SubAreas" dataField="SubAreas"/>
        </mx:columns>
    </mx:DataGrid>  

</s:Application>

Thanks

4

1 回答 1

0

一种解决方案是为您的mx:DataGrid设置一个数据提供程序(如ArrayCollection)作为它的dataProvider属性 - 有关示例 ,请参见此处的将数据传递到 DataGrid 控件部分。

现在,当您的onResult函数执行时,您可能希望首先清除您的数据提供者(从DataGrid中删除任何行) - 如果数据源是ArrayCollection,您将使用removeAll方法。现在从ResultEvent参数获取实际的 Web 服务调用结果(在您的代码中,它是event.result)。您需要知道此结果值的数据类型(它应该是某种列表数据结构),以便弄清楚如何将其元素作为网格行添加到DataGrid 的数据提供程序中。例如,如果您的数据提供者是ArrayCollection,您可以将 的每个元素添加event.result到此ArrayCollection使用它的addItem方法。如果event.result实现IList接口,您可以使用ArrayCollection的addAll方法将所有行添加到数据提供程序中。确保作为行添加到数据提供程序中的对象符合您的mx:DataGridColumns - 即这些对象需要具有AreasSubAreas属性,以便它们的值将显示在这些列中。

这是来自 Adob ​​e 的特定于Web 服务的DataGrid示例。

protected function onResult(event:ResultEvent, token:Object=null):void
{
     // Assuming the grid's data provider is an ArrayCollection

     // (1) clear existing table rows
     dataProvider.removeAll();

     // (2) add the new rows from event.result into dataProvider 
     // using dataProvider.addAll(event.result) if possible or one at a time
     // using dataProvider.addItem(...)
     dataProvider.addAll(event.result);

     // An alternative to the above would be to replace the dataProvider 
     // of your grid with event.result if it is a compatible data type
     //       AreasOfWestBengal.dataProvider = event.result;
}
于 2012-12-13T03:04:57.220 回答