Please ignore the sample I posted earlier. It works but not optimized. Below is an optimized version. Anyone who likes to adopt the logic, may follow this example, but not the first one,
- Code: Select all Expand view
#include "fivewin.ch"
#include "xbrowse.ch"
REQUEST DBFCDX
function Main()
local oWnd, oBrw
fwNumFormat( 'A', .t., .f. )
USE COLIN NEW SHARED VIA 'DBFCDX'
DEFINE WINDOW oWnd
SET MESSAGE OF oWnd TO '' 2007
@ 0,0 XBROWSE oBrw OF oWnd ALIAS Alias() ;
COLUMNS "Code", "DESC", "Qty", "Cost", "Charge" ;
HEADERS , "Description" ;
PICTURES "@!" ;
CELL LINES FOOTERS FASTEDIT
WITH OBJECT oBrw
:nEditTypes := EDIT_GET
:nStretchCol := STRETCHCOL_WIDEST
:lHScroll := .f.
END
WITH OBJECT oBrw:Qty
:nFooterType := AGGR_SUM
:bOnChange := { || oBrw:Charge:PostEdit( oBrw:Qty:Value * oBrw:Cost:Value ) }
END
WITH OBJECT oBrw:Cost
:nFooterType := AGGR_SUM
:bOnChange := { || oBrw:Charge:PostEdit( oBrw:Qty:Value * oBrw:Cost:Value ) }
END
WITH OBJECT oBrw:Charge
:nFooterType := AGGR_SUM
:bEditWhen := { || (oBrw:cAlias)->Qty > 0 }
:bOnChange := { || oBrw:Cost:PostEdit( oBrw:Charge:Value / oBrw:Qty:Value ) }
END
oBrw:MakeTotals()
oBrw:CreateFromCode()
oWnd:oClient := oBrw
ACTIVATE WINDOW oWnd
return 0
From version 11.08, instead of PostEdit, use VarPut.
The method MakeTotals() scans the entire database to compute the totals. This method is not optimized for speed, but intended to provide some computational flexibility for different aggregates like averages, standard deviations, etc and takes into account null values and non-numeric values in some rows. For small and medium sized tables, the performance can be satisfactory, but it is
desirable to use pre-computed totals for medium, and large tables.
That is the reason why should try to avoid using the Method MakeTotals() inside the program every time some values are changed.
When some cells are edited, XBrowse recomputes the totals and refreshes the footers automatically. For this purpose, XBrowse does NOT retotal by calling MakeTotals(). Instead xBrowse increases the oCol:nTotal by the difference between the edited value and the old value. This way, xBrowse's built-in updation of totals is very fast, avoiding any disk access. Same is true even for complex aggregates like averages, standard deviations, etc.
In the revised sample, I avoided calling MakeTotals when cells are edited and instead depended on the xbrowse's internal updating logic.