1

I have a Provider with the following data:

DP.addItem({Vare:"Casual Item", Size, Type:"", Color:TColor, Amount:AmountT, Price:AmountT*79});

I have made a shopping cart (School project) and after I buy a few items, the data above gets put in a datagrid, but the problem here is that I want to list the total price of all the products in the dataprovider above.

So let's say someone buys 10 T-shirts and 10 Phones, 1 phone = $10, 1 T-shirt = $1

In total of $110, but I want something like

trace(price)

And it should be able to list all the numbers collected in the price column of the dataprovider.

If I am unclear, tell me, I will edit this post (if possible).

Werner
  • 2,537
  • 1
  • 26
  • 38
AgentGod
  • 11
  • 2

1 Answers1

1

Checking the documentation for DataProvider:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/data/DataProvider.html

You can access the items that are contained in a data provider by index, by using the DataProvider.getItemAt() method.

Combined with the length property you should be able to use a for loop to go through all the elements of the DP and sum their prices. Here's how to use different kinds of for loops: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcf.html

The first kind looks promising with i counting up from 0.

If you want to do this all in one easy way, put all the above mentioned functionality into a function, pseudo example code:

public function getPrice():Number
{
    // do for loop here

    // sum up all prices

    return sum;
}

You can now use

trace(getPrice);
null
  • 5,207
  • 1
  • 19
  • 35
  • Keep in mind that I have not used the public function before or used external .as files. As I have little experience with Actionscript, can you explain a bit further? – AgentGod Jan 18 '15 at 19:51
  • @AgentGod I'm afraid your own question proves you wrong: addItem is a public function, so you used them before and DataProvider is a class defined in an external .as file. – null Jan 18 '15 at 20:02
  • Everything is coded in the main .fla file. As I have not learned how to use external files yet. I have tried the following; trace(dpz.getItemAt(0).Price); which will get the price for the first one in the list. But I want it to find the row number (dp.length) and then add (summarize) all the numbers. – AgentGod Jan 18 '15 at 20:14
  • @AgentGod addItem is still a public method of the DataProvider class which is defined in its external class file, no matter if you use them from a .fla file or another .as file. Don't worry. Follow the link I provided. Then search for "getItemAt" and read every section containing it. Try to make sense of it. Please explain what part I should explain further. – null Jan 18 '15 at 20:31
  • I really appreciate all the help you are giving me mate. Sorry for the trouble. I do not understand the loop you are talking about. My problem is currently that the amount of price + product is different each time. So I cannot try the following; dp.getItemAt(0).Price dp.getItemAt(1).Price dp.getItemAt(2).Price dp.getItemAt(3).Price And then sum those up, because sometimes there are 5 products. So every time a user buys something, a new item is added to the provider. How can I make that look you were talking about? Do I just try this: public function get price():Number { return sum; } – AgentGod Jan 18 '15 at 20:42
  • Also, when I try your code, I get the following error; Scene 1, Layer 'AS3', Frame 8, Line 18 1114: The public attribute can only be used inside a package. I am a total noob at AS3. Currently trying to learn. – AgentGod Jan 18 '15 at 20:45
  • @AgentGod I edited my answer. You get that error because your code is not in a class. I changed it so it works on the time line now. I also provided a link on how to use for loops. I hope this helps you to accompany for a variable amount of elements in the data provider. Please ask if you are still having problems. – null Jan 18 '15 at 21:00
  • Thanks for the help, this is what I currently have: http://i.gyazo.com/caf3557e081a273aa55e0a0648651e29.png It traces all the prices, but how do I sum them? Also I still get an error when I run that public function. – AgentGod Jan 18 '15 at 21:19
  • Fixed the issue mate (got help from a friend and ofc your help). http://i.gyazo.com/997149249cf63f412c0265d5648b1edf.png That is the code, if anyone wants it in the future. – AgentGod Jan 18 '15 at 21:24
  • @AgentGod good, that's the solution I was telling you about. Keep in mind that it's usually better to return the value (as I suggested) so you can use the function for other purposes than tracing its result. – null Jan 18 '15 at 21:41