PDA

View Full Version : Dataprovider


salim_designer
05-10-2007, 12:09 PM
I am using Dataprovider to provide data to a Datagrid. I am fetching data from an XML file. I can fetch data if I hardcode the Dataprovider but I want to load it as a variable value.

Here is the code that I am using but it is not working:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">
<mx:Script><![CDATA[

public var dp;
public function doSomething():void {
dp = "feedRequest.lastResult.year2.item";
}

]]></mx:Script>
<mx:WipeRight id="myWR" duration="1000"/>
<mx:Fade id="myAlp" alphaFrom="0.0" alphaTo="1.0" duration="2000"/>

<mx:HTTPService
id="feedRequest"
url="xml/index.xml"
useProxy="false"/>
<mx:Panel x="428" y="0" width="500" height="100%" layout="absolute" title="222" creationComplete="doSomething()">
<mx:DataGrid x="20" y="20" id="dgPosts" width="400" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="Expenses" dataField="Expense"/>
<mx:DataGridColumn headerText="Amount" dataField="Amount" width="150"/>
</mx:columns>
</mx:DataGrid>

</mx:Panel>

</mx:Application>

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<year1>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Car</Expense>
<Amount>450</Amount>
</item>
<item>
<Expense>Gas</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Food</Expense>
<Amount>200</Amount>
</item>
<item>
<Expense>House Rent</Expense>
<Amount>250</Amount>
</item>
<item>
<Expense>Electricity</Expense>
<Amount>300</Amount>
</item>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
</year1>
<year2>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Car</Expense>
<Amount>450</Amount>
</item>
<item>
<Expense>Gas</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Food</Expense>
<Amount>200</Amount>
</item>
<item>
<Expense>House Rent</Expense>
<Amount>250</Amount>
</item>
<item>
<Expense>Electricity</Expense>
<Amount>300</Amount>
</item>

</year2>:rolleyes:

dr_zeus
05-10-2007, 05:36 PM
dp = "feedRequest.lastResult.year2.item";

First, remove those quotes. That definitely won't work.

[Bindable]
public var dp;

Make dp bindable so that changes can be detected.

I'm not sure if that's all you will need, but it's a start.

salim_designer
05-11-2007, 11:24 AM
First of all there was a mistake in the code which I have corrected

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()">

to

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="feedRequest.send()" preinitialize="doSomething()" >

I have also added the code:
dp = "feedRequest.lastResult.year2.item";

Now it is showing feedRequest.lastResult.year2.item in the first row of the datagrid :o

hbd
05-11-2007, 12:05 PM
If you don't want to pass String value, Your code should be
dp = feedRequest.lastResult.year2.item

Also your xml probably needs a root tag which holds <year1> and <year2>

salim_designer
05-14-2007, 05:39 AM
This is driving me nuts.

I tried different combinations but in vain. I even tried removing quotes:

dp = feedRequest.lastResult.year2.item;

But it is showing some error message:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at chart2/doSomething()
at chart2/___Application1_preinitialize()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.core::UIComponent/initialize()
at mx.core::Container/initialize()
at mx.core::Application/initialize()
at chart2/initialize()
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()
at mx.managers::SystemManager/::initializeTopLevelWindow()
at mx.managers::SystemManager/::docFrameHandler()


Here is my code again:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="feedRequest.send()" preinitialize="doSomething()" >
<mx:Script><![CDATA[
import mx.controls.Alert;

[Bindable]
public var dp:String;
public function doSomething():void {
dp = "feedRequest.lastResult.year2.item";
}


]]></mx:Script>
<mx:WipeRight id="myWR" duration="1000"/>
<mx:Fade id="myAlp" alphaFrom="0.0" alphaTo="1.0" duration="2000"/>

<mx:HTTPService
id="feedRequest"
url="xml/index.xml"
useProxy="false"/>
<mx:Panel x="428" y="0" width="500" height="100%" layout="absolute" title="222">
<mx:DataGrid x="20" y="20" id="dgPosts" width="400" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="Expenses" dataField="Expense"/>
<mx:DataGridColumn headerText="Amount" dataField="Amount" width="150"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Click" click="Alert.show(dp),dgPosts.dataProvider=dp"/>

</mx:Panel>


<mx:Panel title="Pie Chart">
<mx:PieChart id="myChart"
dataProvider="{feedRequest.lastResult.year1.item}"
showDataTips="true" creationCompleteEffect="{myAlp}"
>
<mx:series>
<mx:PieSeries
field="Amount"
nameField="Expense"
labelPosition="callout"
/>
</mx:series>
</mx:PieChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>
</mx:Application>



XML code


<?xml version="1.0" encoding="utf-8"?>
<year1>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Car</Expense>
<Amount>450</Amount>
</item>
<item>
<Expense>Gas</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Food</Expense>
<Amount>200</Amount>
</item>
<item>
<Expense>House Rent</Expense>
<Amount>250</Amount>
</item>
<item>
<Expense>Electricity</Expense>
<Amount>300</Amount>
</item>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
</year1>
<year2>
<item>
<Expense>Taxes</Expense>
<Amount>500</Amount>
</item>
<item>
<Expense>Bills</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Car</Expense>
<Amount>450</Amount>
</item>
<item>
<Expense>Gas</Expense>
<Amount>100</Amount>
</item>
<item>
<Expense>Food</Expense>
<Amount>200</Amount>
</item>
<item>
<Expense>House Rent</Expense>
<Amount>250</Amount>
</item>
<item>
<Expense>Electricity</Expense>
<Amount>300</Amount>
</item>

</year2>



I want a very simple thing to be done. Just storing a value for dataprovider in var dp and using it as dataprovider for datagrid.

Please let me know if it is possible in Flex.

salim_designer
05-14-2007, 03:14 PM
Can anyone help me with this. This is really driving me nuts

dr_zeus
05-14-2007, 06:18 PM
You need to remove the quotes. The reason that string is showing up in your DataGrid is because it is a string, and that's all the DataGrid knows to be able to handle it.

I recommend going through the debugger to determine where the error is happening. It might have something to do with the fact that your XML is not actually valid (two root nodes: year1 and year2).

salim_designer
05-15-2007, 05:16 AM
I removed the quotes but still it is not working.

It is showing some error message:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at chart2/doSomething()
at chart2/___Application1_preinitialize()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.core::UIComponent/initialize()
at mx.core::Container/initialize()
at mx.core::Application/initialize()
at chart2/initialize()
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()
at mx.managers::SystemManager/::initializeTopLevelWindow()
at mx.managers::SystemManager/::docFrameHandler()

--------

Am I doing something wrong while calling the functions.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="feedRequest.send()" preinitialize="doSomething()" >

I checked the XML file. I did not find any mistake. Besides, when I do hardcode coding for button:

click="dgPosts.dataProvider=feedRequest.lastResult.year1. item"

It is working fine. But I can't find out, how to get the value using variable

salim_designer
05-15-2007, 05:30 AM
Finally I got it.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="doSomething()" preinitialize="feedRequest.send()" >

We should have preinitialize the xml feed then call doSomething() function at applicationComplete.

Thank you for your help.

dr_zeus
05-15-2007, 05:30 PM
Actually, you may run into errors with that method as well. Your doSomething method sets the data provider, but the feedRequest may not have completed when applicationComplete is called. Instead, you should listen for a complete event from feedRequest itself before calling doSomething.

salim_designer
05-16-2007, 05:35 AM
you should listen for a complete event from feedRequest itself before calling doSomething

How can I do that?

dr_zeus
05-16-2007, 05:46 PM
Actually, I was wrong. It's the result event that you want.

<mx:HTTPService
id="feedRequest"
url="xml/index.xml"
useProxy="false"
result="doSomething()"/>

salim_designer
05-17-2007, 05:15 AM
Wow !!

Thank you very much for your help.

:)