I asked Nages to make these changes many months ago, basically I need to display n columns but at the beginning only n default columns, the user can insert them online or remove them. Obviously I have already made these changes by bringing Francisco's idea but every time there is a new release I would have to rewrite part of the xbrowse class
the changes are simple,
you need to add 3 data
DATA aCols2Hide AS ARRAY INIT Array(0)
DATA aDefaultCols INIT ARRAY( 0 )
DATA aPreviousCols INIT ARRAY( 0 )
and a method SetUserCols(lSetUserCol) and change the method
I made a small xbrowse subclass
- Code: Select all Expand view RUN
- CLASS TMyBrowse FROM TXBrowse
CLASSDATA lRegistered AS LOGICAL
DATA aCols2Hide AS ARRAY INIT Array(0)
DATA aDefaultCols INIT ARRAY( 0 )
DATA aPreviousCols INIT ARRAY( 0 )
METHOD New (nRow, nCol, nWidth, nHeight, oWnd)
METHOD SetUserCols(lDefCols)
ENDCLASS
METHOD New(nRow, nCol, nWidth, nHeight, oWnd) CLASS TMyBrowse
return ::SuPer:New()
METHOD SetUserCols(lDefCols) CLASS TMyBrowse
local aCols
DEFAULT lDefCols := .t.
aCols := if(lDefCols, ::aDefaultCols, ::aPreviousCols )
if Len( aCols ) != 0
AEval( ::aCols, { |o| If( AScan( aCols, o:nCreationOrder ) > 0, o:Show(),o:Hide() ) } )
endif
RETURN NIL
and modified with ovverride command the method SetColumns
- Code: Select all Expand view RUN
- #include "FiveWin.ch"
#xcommand OVERRIDE METHOD <!Message!> [IN] CLASS <!Class!> ;
WITH [METHOD] <!Method!> [SCOPE <Scope>] => ;
__clsModMsg( <Class>():classH, #<Message>, @<Method>() )
function Ut_Xbrowse_override()
OVERRIDE METHOD SetColumns IN CLASS TXbrowse WITH SetMyColumns
return nil
//----------------------------------------------------------------------------------------//
Function SetMyColumns(nRow, nCol, nFlags, oColumn )
local Self := HB_QSelf()
local oMenu, oCol, cPrompt
local nFor, nLen
local cPrvGrp := ""
local oGrpMenu
local lChecked , lDefCols
local bPrevCols := {|| ::aPreviousCols := ::GetDisplayCols() }
local bUserCols := {|| aCols := if(lDefCols, ::aDefaultCols, ::aPreviousCols ) ,;
if( Len( aCols ) != 0, AEval( ::aCols, { |o| If( AScan( aCols, o:nCreationOrder ) > 0, o:Show(),o:Hide() ) } ) ,)}
nLen := Len( ::aCols )
MENU oMenu POPUP
if ::l2000
OSend( oMenu, "_L" + Str( Min( 2015, ::n2KStyle ), 4 ), .t. )
else
oMenu:l2013 := .t.
endif
Menuitem fwstring("Show/Hide") SEPARATOR BOLD COLORPNEL CLR_BLACK, ::nRecSelColor
separator
for nFor := 1 to nLen
oCol := ::aCols[ nFor ]
if !( IfNil( oCol:cGrpHdr, "" ) == cPrvGrp )
if ! Empty( cPrvGrp )
ENDMENU
oGrpMenu := nil
endif
if ! Empty( oCol:cGrpHdr )
MENUITEM oCol:cGrpHdr BOLD
MENU
MENUITEM oGrpMenu PROMPT oCol:cGrpHdr BOLD ;
ACTION ( lChecked := oMenuItem:lChecked, ;
AEval( oMenuItem:Cargo, ;
{ |o| If( lChecked, o:Hide(), o:Show() ) } ) )
oGrpMenu:Cargo := {}
SEPARATOR
endif
cPrvGrp := IfNil( oCol:cGrpHdr, "" )
endif
MenuAddItem( If( Empty( oCol:cHeader ), "Col-" + cValToChar( nFor ), oCol:cHeader ), ;
nil, !oCol:lHide, ;
( Len(::aDisplay) != 1 .or. ocol:nPos != 1 ), ;
{ |o| If( o:Cargo:lHide, o:Cargo:Show(), o:Cargo:Hide() ) } ):Cargo := oCol
if oGrpMenu != nil
AAdd( oGrpMenu:Cargo, oCol )
if !oCol:lHide
oGrpMenu:lChecked := .t.
endif
endif
next
if ! Empty( cPrvGrp )
ENDMENU
endif
if oColumn != nil .and. !Empty( oColumn:bHRClickMenu )
Eval( oColumn:bHRClickMenu, oColumn, oMenu )
endif
separator
if Len( ::aDefaultCols ) != 0
Menuitem fwstring("Default Columns") ACTION ( Eval( bPrevCols ), lDefCols:=.t., Eval( bUserCols ), ::Refresh() )
Menuitem fwstring("All Columns") ACTION ( Eval( bPrevCols ), AEval( ::aCols, { |o| o:lHide := .f. } ) , ::Refresh() )
Menuitem fwstring("Previous Columns") ACTION ( lDefCols:=.f., Eval( bUserCols ), ::Refresh() )
endif
ENDMENU
ACTIVATE POPUP oMenu AT ::FirstRow(), nCol OF Self
return nil
Usage:
local aDefCols := {1, 2, 4 } // columns I wish see at init
@ 110,10 XBROWSE oBrw SIZE -5,-60 PIXEL OF oDlg ;
DATASOURCE oDbf ;//
COLUMNS "First","Last","Street", "City","State","Zip","Hiredate","Married", "Age" ; //
CELL LINES AUTOSORT NOBORDER FOOTERS CLASS TmyBrowse()
WITH OBJECT oBrw
:RestoreState( cState )
:aDefaultCols := aDefCols
:SetUserCols(lSetUserCol)
:lIncrFilter := .t.
:bOnSort := { |b,oCol| oBrw:Seek( "" ), ;
oBrw:cFilterFld := oCol:cExpr, ;
oBrw:SetFocus() }
:lDrawBorder := .t.
:lHScroll := .f.
:SetMultiSelectCol()
Seek := oGet
:CreateFromCode()
END