Footer en TBrowse
Publicado: Jue Dic 26, 2019 10:09 pm
por Ariel
Hola,
como hago para totalizar una columna de un browse? ya puse el lFooter en .t., pero no encuentro como ponerle a la columna especifica que totalice ?
Gracias
Re: Footer en TBrowse
Publicado: Mar Ene 21, 2020 3:57 pm
por bingen
Nosotros sobrecargamos el browse para ello:
Method SetFooter( xCol, xValue ) Class TBrowse
Local nCol:=0
::lFooter := .T. //Por si se nos ha olvidado
If ValType(xCol)<>"A"
xCol:={xCol}
Endif
If ValType(xValue)<>"A"
xValue:={xValue}
Endif
For nCol:=1 to Len(xCol)
If ValType(xCol[nCol])="N"
xCol[nCol]:=::aCols[xCol[nCol]]:cHeader
Endif
If ::ColWithHeader(xCol[nCol])<>Nil
::ColWithHeader(xCol[nCol]):cFooter :=AllString(xValue[nCol])
Else
Logdebug("ERROR tBrowse:SetFooter no encuentra la columna "+xCol[nCol])
Endif
Next
Return Nil
Y luego también hicimos otras sobrecarga para sumar, contar, promediar, etc columnas de tDBBROWSE
//Devuelve la suma del campo o array de campos xFieldName que esta/n en el oDataSet del browse
METHOD Sum( xFieldName, lFormatEuro ) CLASS TDBBrowse
LOCAL aDatos:={}, nCampo:=0, aResult:={},nC:=0
Default lFormatEuro To .F.
If ValType(xFieldName)<>"A"
xFieldName:={xFieldName}
Endif
aResult:=Array(Len(xFieldName))
AFill(aResult,0)
IF ::oDataSet == Nil .OR. !::oDataset:lOpen
Return IF(Len(aResult)=1,aResult[1],aResult)
ENDIF
For nCampo:= 1 to Len(xFieldName)
aDatos :={}
nC := ::oDataSet:FieldPos(xFieldName[nCampo])
If nC > 0
if ::oDataSet:oRecords:ClassName() == "TWebRecords" .Or. ::oDataSet:oRecords:ClassName() == "TMemRecords"
aDatos := ArrayCol(::oDataSet:oRecords:aWork,nC)
Else
aDatos := ArrayCol(::oDataSet:oRecords:aData,nC)
ENdif
AEval(aDatos,{|x| aResult[nCampo]+=Val(ToString(x))})
aResult[nCampo]:=If(!lFormatEuro,aResult[nCampo],FormatEuro(aResult[nCampo]))
Else
LogDebug("No existe el campo "+xFieldName[nCampo]+" a Sumar en el DbBrowse.")
Endif
Next
aDatos :={}
Return IF(Len(aResult)=1,aResult[1],aResult)
//Devuelve la cuenta del campo o array de campos xFieldName que esta/n en el oDataSet del browse
METHOD Count( xFieldName ) CLASS TDBBrowse
LOCAL aDatos:={}, nCampo:=0, aResult:={},nC:=0
If ValType(xFieldName)<>"A"
xFieldName:={xFieldName}
Endif
aResult:=Array(Len(xFieldName))
AFill(aResult,0)
IF ::oDataSet == Nil .OR. !::oDataset:lOpen
Return IF(Len(aResult)=1,aResult[1],aResult)
ENDIF
For nCampo:= 1 to Len(xFieldName)
aDatos :={}
nC := ::oDataSet:FieldPos(xFieldName[nCampo])
If nC > 0
aDatos := ArrayCol(::oDataSet:oRecords:aData,nC)
AEval(aDatos,{|x| aResult[nCampo]+=IF(!Vacio(x),1,0)})
Else
LogDebug("No existe el campo "+xFieldName[nCampo]+" a Sumar en el DbBrowse.")
Endif
Next
aDatos :={}
Return IF(Len(aResult)=1,aResult[1],aResult)
//Devuelve la media aritmética del campo o array de campos xFieldName que esta/n en el oDataSet del browse
METHOD Avg( xFieldName, lFormatEuro, nDecimales ) CLASS TDBBrowse
LOCAL aDatos:={}, nSuma:=0, nCuenta:=0, nCampo:=0, aResult:={} ,nC:=0
Default lFormatEuro To .F.
Default nDecimales To 2
If ValType(xFieldName)<>"A"
xFieldName:={xFieldName}
Endif
aResult:=Array(Len(xFieldName))
AFill(aResult,0)
IF ::oDataSet == Nil .OR. !::oDataset:lOpen
Return IF(Len(aResult)=1,aResult[1],aResult)
ENDIF
For nCampo:= 1 to Len(xFieldName)
aDatos :={}
nC := ::oDataSet:FieldPos(xFieldName[nCampo])
If nC > 0
aDatos := ArrayCol(::oDataSet:oRecords:aData,nC)
AEval(aDatos,{|x| nSuma+=Val(ToString(x))})
AEval(aDatos,{|x| nCuenta+=IF(!Vacio(x),1,0)})
aResult[nCampo]:=If(!lFormatEuro,Round(nSuma/nCuenta,nDecimales),FormatEuro(nSuma/nCuenta))
Else
LogDebug("No existe el campo "+xFieldName[nCampo]+" a Sumar en el DbBrowse.")
Endif
Next
aDatos :={}
Return IF(Len(aResult)=1,aResult[1],aResult)
//Devuelve el valor mínimo del campo cFieldName que esta en el oDataSet del browse
METHOD Min( cFieldName, lFormatEuro, lNoCero ) CLASS TDBBrowse
LOCAL aDatos:={}, nMin:=9999999999999999
Default lFormatEuro To .F.
Default lNoCero To .F.
IF ::oDataSet == Nil .OR. !::oDataset:lOpen
Return nMin
ENDIF
with object ::oDataSet
:SaveState(.T.)
aDatos:=::oDataSet:SaveToArray( cFieldName )
:RestoreState(.T.)
END WITH
If ::oDataSet:FieldExist(cFieldName)
AEval(aDatos,{|x| nMin:=IF(lNoCero .And. Val(ToString(x[1]))=0,nMin,Min(nMin,Val(ToString(x[1])))) })
Else
LogDebug("No existe el campo "+cFieldName+" a calcular el mínimo en el DbBrowse")
Endif
nMin:=IF(nMin=9999999999999999,0,nMin)
aDatos :={}
Return If(!lFormatEuro,nMin,FormatEuro(nMin))
//Devuelve el valor máximo del campo cFieldName que esta en el oDataSet del browse
METHOD Max( cFieldName, lFormatEuro ) CLASS TDBBrowse
LOCAL aDatos:={}, nMax:=0
Default lFormatEuro To .F.
IF ::oDataSet == Nil .OR. !::oDataset:lOpen
Return nMax
ENDIF
with object ::oDataSet
:SaveState(.T.)
aDatos:=::oDataSet:SaveToArray( cFieldName )
:RestoreState(.T.)
END WITH
If ::oDataSet:FieldExist(cFieldName)
AEval(aDatos,{|x| nMax:=Max(nMax,Val(ToString(x[1])))})
Else
LogDebug("No existe el campo "+cFieldName+" a calcular el máximo en el DbBrowse")
Endif
aDatos :={}
Return If(!lFormatEuro,nMax,FormatEuro(nMax))
Re: Footer en TBrowse
Publicado: Mar Ene 21, 2020 9:25 pm
por ignacio
Hola Ariel,
Código: Seleccionar todo
LOCAL nValue := 0
WITH OBJECT ::oDbfDataSet1
:SaveState( .T. )
:GoTop()
DO WHILE !:Eof()
nValue += :Salary
:Skip()
ENDDO
:RestoreState( .T. )
END WITH
::oDBBrowse1:aCols[?]:cFooter := Transform( nValue, "99,999,999.99" )
Saludos
Re: Footer en TBrowse
Publicado: Mié Ene 22, 2020 7:41 pm
por Ariel
Bingen e Ignacio,
Muchas gracias