Hi Guys,
I have a datagrid, which I have as a form. Each row contains two fields, description and order_price. What I would like to do is sum the order_price for each row every time it is changed and place it in a field called total_price.
I'm guessing I need to execute the script onTextChanged which is fine. What I can't seem to figure out is how to do the actual calculation, i.e. retrieve the data from the datagrid. I found this tutorial http://lessons.runrev.com/s/lessons/m/d ... or-columns, but its mainly for table based data grid, does anyone have any form related tutorial/samples?
Many thanks
Jalz
Calculate grand total for line prices in a data grid
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Calculate grand total for line prices in a data grid
Hi.
Check out the dataGrid guide. You will want to look at the "dgText" or the "dgData". Either of these will retrieve the contents of the data grid, the first in the clear and the second in an array. Once you have that, you are home free. For example, if you just get the dgText, this will be read as a tab and return delimited dataset, and you can extract the items desired in each line and do the math.
If I read your post right, you want to sum an entire column of data on each change? In this case, you have to crunch the data. There are built-in properties that will return the data in a row (dgDataOfLine), but I do not think (could be wrong) there is one that will do so for a column. I think this is just because that is how datasets are organized. See p. 94 of the guide...
Craig Newman
Check out the dataGrid guide. You will want to look at the "dgText" or the "dgData". Either of these will retrieve the contents of the data grid, the first in the clear and the second in an array. Once you have that, you are home free. For example, if you just get the dgText, this will be read as a tab and return delimited dataset, and you can extract the items desired in each line and do the math.
If I read your post right, you want to sum an entire column of data on each change? In this case, you have to crunch the data. There are built-in properties that will return the data in a row (dgDataOfLine), but I do not think (could be wrong) there is one that will do so for a column. I think this is just because that is how datasets are organized. See p. 94 of the guide...
Craig Newman
Re: Calculate grand total for line prices in a data grid
Hi Jalz,
I would rather use a "closefield" handler to call your calculating routine.
Calculate like this, maybe put it into the card script:
...and call AFTER the datagrid has been updated:
Best
Klaus
I would rather use a "closefield" handler to call your calculating routine.
Calculate like this, maybe put it into the card script:
Code: Select all
function get_order_price
put the dgData of grp "your datagrid here" into tData
put 0 into tTotal
repeat for each key tkey in tData
add tData[tKey]["order_price"] to tTotal
end repeat
return tTotal
end get_order_price
Code: Select all
...
put get_order_price() into fld "total_price"
...
Klaus
Re: Calculate grand total for line prices in a data grid
Hi Klaus,
Thank you for the code snippet. I've created a simple sample file, but I cant seem to get the value of order_price from the data grid (bear in mind I am using a form). Any ideas what I'm missing?
Many thanks
Jalz
Thank you for the code snippet. I've created a simple sample file, but I cant seem to get the value of order_price from the data grid (bear in mind I am using a form). Any ideas what I'm missing?
Many thanks
Jalz
- Attachments
-
- mycalc.livecode.zip
- (4.53 KiB) Downloaded 206 times
Re: Calculate grand total for line prices in a data grid
Hi jalz,
your problem is that you have EDITABLE fields in your datagrid and
no script to add the entered data to the datagrid!
So getting "the dagdata..." will always be empty and this "total_price"!
And you did not prepare e.g. the "FillInData" handler in the "row behavior"
to fill your datagrid correctly!
Best
Klaus
your problem is that you have EDITABLE fields in your datagrid and
no script to add the entered data to the datagrid!
So getting "the dagdata..." will always be empty and this "total_price"!
And you did not prepare e.g. the "FillInData" handler in the "row behavior"
to fill your datagrid correctly!
Best
Klaus