- Code: Select all Expand view RUN
oBrw:SetTree( oTree, [aBitmaps], [bOnSkip], [aCols] ).
Building a tree from a sorted set of data is a bit complex. Now, the method SetTree() simplifies the process by automating it.
Process:
First set up browse of the sorted data in the normal way from any datasource, viz., Array, DBF, Object, ADO of oQry.
Then, either during run-time or before, call oBrw:SetTree( [<nLevels>] ). nLevels defaults to 2. XBrowse automatically builds a tree object from the data using leftmost sorted columns up to the specified number of levels and also switches the browse to tree-view. We do not need to write any code for building the tree.
\fwh\samples\xbrtree.prg
You can see how simple it is to display the tree-view of any sorted data. This program also demonstrates how to toggle between tree-view and normal view.
- Code: Select all Expand view RUN
- #include "fivewin.ch"
REQUEST DBFCDX
//----------------------------------------------------------------------------//
function Main()
RDDSETDEFAULT( "DBFCDX" )
TestTree1()
TestTree2()
return nil
//----------------------------------------------------------------------------//
function TestTree1()
local oDlg, oBrw
USE CUSTOMER NEW
SET ORDER TO TAG STATE
GO TOP
DEFINE DIALOG oDlg SIZE 800,400 PIXEL TRUEPIXEL
@ 60,20 XBROWSE oBrw SIZE -20,-20 PIXEL OF oDlg ;
DATASOURCE Alias() ;
COLUMNS "STATE", "CITY", "STREET", "ZIP", "AGE" ;
CELL LINES NOBORDER
oBrw:lDisplayZeros := .f.
oBrw:CreateFromCode()
@ 10, 20 BTNBMP PROMPT "TREE" SIZE 150,30 PIXEL OF oDlg FLAT ;
WHEN oBrw:nDataType == DATATYPE_RDD ;
ACTION oBrw:SetTree( 3, { 0x30082, 0x30084, 0x20097 } )
@ 10,200 BTNBMP PROMPT "DBF" SIZE 150,30 PIXEL OF oDlg FLAT ;
WHEN oBrw:nDataType != DATATYPE_RDD ;
ACTION ( CUSTOMER->( oBrw:SetRDD( nil, nil, { "STATE", "CITY", "STREET", "ZIP", "AGE" } ) ), ;
oBrw:GoTop() )
ACTIVATE DIALOG oDlg CENTERED
CLOSE CUSTOMER
return nil
function TestTree2()
local oDlg, oFont, oBrw
local aData, aTotal
local aCols := { "1 AS State", "2 AS City", "3 AS Street", "4 AS Age", "5 AS Salary" }
USE CUSTOMER NEW
aData := FW_DbfToArray( "STATE,TRIM(CITY),TRIM(STREET),AGE,SALARY" )
CLOSE CUSTOMER
aData := FW_ArrGroupSum( aData, 1, , { 4, 5 } )
ASORT( aData, , , { |x,y| If( x[ 1 ] == y[ 1 ], x[ 2 ] < y[ 2 ], x[ 1 ] < y[ 1 ] ) } )
aTotal := aData[ 1 ]
ADel( aData, 1, .t. )
DEFINE FONT oFont NAME "TAHOMA" SIZE 0,-14
DEFINE DIALOG oDlg SIZE 800,400 PIXEL TRUEPIXEL FONT oFont
@ 60,20 XBROWSE oBrw SIZE -20,-20 PIXEL OF oDlg ;
DATASOURCE aData COLUMNS aCols ;
CELL LINES NOBORDER FOOTERS
oBrw:cFooters := aTotal
oBrw:CreateFromCode()
@ 10, 20 BTNBMP PROMPT "TREE" SIZE 150,30 PIXEL OF oDlg FLAT ;
WHEN oBrw:nDataType == DATATYPE_ARRAY ;
ACTION ( oBrw:SetTree( nil, { 0x30082, 0x30084, 0x20097 } ) )
@ 10,200 BTNBMP PROMPT "ARRAY" SIZE 150,30 PIXEL OF oDlg FLAT ;
WHEN oBrw:nDataType != DATATYPE_ARRAY ;
ACTION ( oBrw:SetArray( aData, nil, nil, aCols ), oBrw:cFooters := aTotal )
ACTIVATE DIALOG oDlg CENTERED
RELEASE FONT oFont
return nil
The Second example also demonstrates the use of FW_ArrGroupSum() to automatically calculate group and grand totals of a sorted array.