Calculate grand total for line prices in a data grid

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Calculate grand total for line prices in a data grid

Post by jalz » Sun Jan 05, 2014 11:53 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Calculate grand total for line prices in a data grid

Post by dunbarx » Mon Jan 06, 2014 12:35 am

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Calculate grand total for line prices in a data grid

Post by Klaus » Mon Jan 06, 2014 1:36 am

Hi Jalz,

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
...and call AFTER the datagrid has been updated:

Code: Select all

...
put get_order_price() into fld "total_price"
...
Best

Klaus

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Calculate grand total for line prices in a data grid

Post by jalz » Tue Jan 07, 2014 2:00 pm

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
Attachments
mycalc.livecode.zip
(4.53 KiB) Downloaded 206 times

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Calculate grand total for line prices in a data grid

Post by Klaus » Tue Jan 07, 2014 2:10 pm

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

Post Reply