Page 1 of 1

Calculated fields in Detail View (DV)

Posted: 2014-04-03 13:15
by adz1111
Hi

I'm very new to AppGini (few days) - and so far the experience has been very good :-)

However, I am struggling on one thing. I have a Table View of weekly timesheets. In this view they list dates, who the client was etc. But it also has also has 2 calculated fields on the right - TotalHours and Total Days (calculated from the individual "daily hours" fields; hidden in the Table View). Now, these totals are calculated on the fly (via tblTimesheets_init). The actual underlying table has those 2 fields as placeholders only. They are empty and I wish to keep it that way; I do not want to store those values in the database itself. This all works absolutely fine. See below...
TV.JPG
TV.JPG (51.26 KiB) Viewed 5450 times
However, when I click on a timesheet to see its Detail View I want to do the same thing. That is, show an on the fly calculations as per its Table View. But I cannot see anything that allows me to access a Datalist object in Detail View like I can in Table View. Here is a mock up of what I'm trying to do...
DV.JPG
DV.JPG (62.16 KiB) Viewed 5450 times
In advance, any thoughts very gratefully received.

Many thanks.

Re: Calculated fields in Detail View (DV)

Posted: 2014-04-04 20:07
by adz1111
Never mind folks. I figured out what I needed to do.

For those interested it is in the tablename_dv() hook in the tablename.php file where it is done after all. I was confused as this hook only provides you the page's generated HTML and no actual data per se. Penny dropped when I realised there was nothing stopping me running a query in that hook function to get the data, especially as the primary key is ONE of the things provided to the hook.

The code I added now does the calc when viewing a timesheet you have clicked on in the Table View. If in the Table View you click to add a new record, then the code adds a caption for those fields in the detail view indicating a value will appear for those fields after the timesheet is added / updated.

As I said, for those interested, the code is below:

Code: Select all

function tbltimesheets_dv($selectedID, $memberInfo, &$html, &$args)
	{
		if ($selectedID)					// if viewing an existing record then do this lot
		{
			$id=makeSafe($selectedID);
			
			// get the sum of all the hours for this timesheet - will only return 1 row of 1 column
			$res = sql(
				"SELECT (tbltimesheets.MondayHours + tbltimesheets.TuesdayHours + ".
					"tbltimesheets.WednesdayHours + tbltimesheets.ThursdayHours + ".
					"tbltimesheets.FridayHours + tbltimesheets.SaturdayHours + ".
					"tbltimesheets.SundayHours) AS ToThours ".
				"FROM tbltimesheets ".
				"WHERE tbltimesheets.TimesheetID = '$id'"	
				, $eo);
			
			if (mysql_num_rows($res))					// SQL returned one or more rows?
			{
				$row = mysql_fetch_row($res);

				$th = sprintf("%.2f", $row[0]);			// Get total hours 
				$td = sprintf("%.2f", ($th / 8));		// Get total days 
				
				$frh = 'id="TotHours"></p>';			// Set up target "hours" HTML to change
				$toh = 'id="TotHours">'.$th.'</p>';		// Set up the "hours" HTML the target will be changed too
				
				$frd = 'id="TotDays"></p>';				// Set up target "days" HTML to change
				$tod = 'id="TotDays">'.$td.'</p>';		// Set up the "days" HTML the target will be changed too
				
				$html = str_replace($frh, $toh, $html);
				$html = str_replace($frd, $tod, $html);
			}
		}
		else									// otherwise, if viewing a "new" empty record do this
		{
			$frh = 'id="TotHours"></p>';
			$toh = 'id="TotHours">Automatically calculated after a timesheet is submitted or updated</p>';
			
			$frd = 'id="TotDays"></p>';
			$tod = 'id="TotDays">Automatically calculated after a timesheet is submitted or updated</p>';
			
			$html = str_replace($frh, $toh, $html);
			$html = str_replace($frd, $tod, $html);
		}

	}
:-)

Re: Calculated fields in Detail View (DV)

Posted: 2014-04-05 08:40
by a.gneady
Thanks for sharing :D

Re: Calculated fields in Detail View (DV)

Posted: 2014-04-05 10:04
by adz1111
Not a problem at all - I'm sure I'm not the only one who had that d'oh moment! :-)