View Full Version : DataGrid Totals?
darthmahon
April 1st, 2005, 06:04 AM
Hi,
I want to sum up a total of a specific column in a datagrid but am having problems, I'm using Flash MX 2004 Pro.
Here is what I have done:
Inserted a DataGrid, named it UserGrid.
Created a dynamic textfield called total.
I have the folllowing AS on frame 1:
import mx.controls.gridclasses.DataGridColumn;
UserGrid.addColumn(new DataGridColumn("Title"));
UserGrid.addColumn(new DataGridColumn("Quantity"));
UserGrid.addColumn(new DataGridColumn("Price"));
onClipEvent(enterFrame){
sum = 0
for(i=0; i<UserGrid.getLength() ; i++) {
trace(UserGrid.getItemAt(i).Price) //Here I output column values
sum += Number(UserGrid.getItemAt(i).Price)
}
total.text = sum
}
I also have an add button that has the following AS:
on (release) {
UserGrid.addItem({Title:"Product 1", Quantity:"1", Price:"27"});
}
Now, when I press the add button it puts a new entry into the datagrid, but I want it to calculate the total of all the rows in the Price column and insert them into the textfield total.
Not sure if my script on frame 1 is correct to be honest, gives me this in the textfield at the moment:
_level0.total
Any ideas?
Cheers,
Chris
Shimi
April 1st, 2005, 10:30 AM
well i dont understand the problem...
your onRelease function DOES create a new row because that what addItem function supposed to do..
if you want that on release will show you the sum all you need to do is copy
the script from the onClipEvent and put it on the onRelease function like so:
(In your button of course... and remove the onClipEvent)
on (release) {
sum = 0;
for(i=0; i<UserGrid.getLength() ; i++) {
trace(UserGrid.getItemAt(i).Price); //Here I output column values
sum += Number(UserGrid.getItemAt(i).Price);
}
total.text = sum;
}
darthmahon
April 1st, 2005, 03:47 PM
Hi,
Thanks for the reply. I think I need to explain my problem a bit more:
I have the datagrid and the columns set up fine, I'm happy with that.
I have an add button that adds rows to the datagrid absolutely fine also.
But, as I am adding rows, it is not calculating the total of all the "Price" rows.
So for example I have these two values in the datagrid:
TITLE | QUANTITY | PRICE
Test1 | 2............ | 12
Test2 | 1............ | 30
Now, I press the add button, it adds a new item with a Title of "Test3", Quantity of "1" and Price of "3". Please excuse the dots (.....).
So I need to add up ALL of the values within the "PRICE" column and display it in the Total text field on the fly, so basically it needs to keep checking it to make sure the values are added up properly.
From your example it seems I can just add them up while I am adding new items. But for some reason my calculations and references to text fields are not working.
I hope that clears it up, I think what I am asking for is quite simple.
Cheers,
Chris
Shimi
April 1st, 2005, 03:57 PM
ok try this
get rid of the onClipEvent
and in the relase event do both, add and sum
on (release) {
UserGrid.addItem({Title:"Product 1", Quantity:"1", Price:"27"});
sum = 0;
for(i=0; i<UserGrid.getLength() ; i++) {
trace(UserGrid.getItemAt(i).Price); //Here I output column values
sum += Number(UserGrid.getItemAt(i).Price);
}
total.text = sum;
}
if its still doesnt work just throw me the files ill check it out
darthmahon
April 2nd, 2005, 03:47 AM
Hi Shimi,
Thanks that worked to some extent, but the problem is as I am adding these items, it is not calculating the total value of all the items in the price column.
I have uploaded the file to this location:
http://yduk.net/shopcart.fla
Hope it makes sense, I need it to calculate the total of all the values in the Price column only.
Also, if you have any ideas as to why my total value is initally set to _level0.total, let me know, because I set the value to 0 on the first frame but then when I added items it didn't work anymore.
Cheers,
Chris
Shimi
April 2nd, 2005, 04:10 AM
Chris you will never beleave why it wont work lol
the text field total have an instance name "total" and a var name ...
just clear the var name at the textfield properties and everything will work
and it shows "_level0.total" because of that eather lol
darthmahon
April 2nd, 2005, 04:15 AM
LOL - :beam: Works fine now yea, thanks a lot for your help :)
Just one more quick question, do you have any idea how I would go about checking to see if an item has already been added to the datagrid?
For example I add Product 1 with a price of 20, I then add it again, but instead of it adding it twice to the datagrid, I want it to replace the first instance of it and increment the quantity to 2. Make sense?
Cheers,
Chris
Shimi
April 2nd, 2005, 04:30 AM
You can create another variable to the grid named pID (Product ID..)
dont create a column for it because its not supposed to be visible in the grid
just add it when you add item like so:
UserGrid.addItem({pID:1,name:"Product 1", quantity:"1", price:"27"]);
Now, every product will have its own id you could of chose name to check duplicates but i think id is better for that
now when you add the prod go over the items in the grid with for loop and check if the id is identical to the when that your adding now and if its true just += 1 the quantity
darthmahon
April 2nd, 2005, 04:33 AM
Hi Shimi,
That's excellent - thanks for all your help! :)
Have a good day.
Cheers,
Chris
Shimi
April 2nd, 2005, 04:52 AM
You too Chris!
darthmahon
April 2nd, 2005, 12:35 PM
Hi Shimi,
I've been trying to search through the datagrid to find existing items but I am having problems again :(
I have this code on a button that adds an item to the datagrid:
on (release) {
// loop through dataset to see if entry already exists
for(i=0; i<UserGrid.getLength() ; i++) {
// entry exists, update quantity
if (UserGrid.getItemAt(i).pID == 2) {
UserGrid.editField(2, "quantity", "2"});
// entry does not exist, add new
} else {
UserGrid.addItem({pID:2, name:"Product 1", quantity:"1", price:"27"});
}
// calculate totals
sum = 0;
for(i=0; i<UserGrid.getLength() ; i++) {
trace(UserGrid.getItemAt(i).name); //Here I output column values
sum += Number(UserGrid.getItemAt(i).price);
}
// output totals
total.text = sum;
}
}
Doesn't seem to work...not sure if I am using the for loop properly and then replacing the item.
Oh if this was PHP, would be easier for me, I guess it's just another learning curve but I can't seem to take the same logic as I would from PHP into Flash :(
Any ideas?
Cheers,
Chris
Shimi
April 2nd, 2005, 01:06 PM
can you send me the FLA it would of be much much easer..
darthmahon
April 2nd, 2005, 02:02 PM
Hi Shimi,
You can download the file at the following location:
http://www.yduk.net/shopcart.fla
Once again, thanks for your time.
Cheers,
Chris
Shimi
April 2nd, 2005, 04:24 PM
Ok it goes like this:
1 - when you check if any records found it the grid -> sign was opposite..
2 - you had to convert the current quantity to number when reading from object
3 - you cannot do
somevar = anothervar++;
//You can do
somevar = anothervar+1;
//Or
anothervar++;
4 - you if statement for checking if the pId = 2 was corrent BUT you did an else which were adding new row and this is wrong
because it will and a new row for each row that wasnt pId = 2 (because its in the for loop...)
see the solution in the attached file..
5 - i created a function which will calculate the sum so you can call it whenever you want
6 - i added to that function the multiply of price*quantity..
Thats all!
http://phpisrael.dotgeek.org/shopcart.fla
darthmahon
April 3rd, 2005, 06:16 AM
Hi Shimi,
WOW! That's fantastic, I see now what I was doing wrong and will definately remember these mistakes. I'm not used to coding in ActionScript but I can definately see how things are stating to work...
Only other thing I need to do now is to allow the user to change quantities on items depending on what they click on but I'm going to give this a go now.
Once again thanks for all your help, you've been blimin marvelous!! :) :) :) :thumb:
Shimi
April 3rd, 2005, 07:44 AM
I'm always here to help Chris!
:)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.