Hello,
I copied this .prg long time back from a forum. Can I convert this into class, if so how can?
I just wanted to drop the class into any grid contain form and it should sort accordingly.
Please guide me.
Thanks
I copied this .prg long time back from a forum. Can I convert this into class, if so how can?
I just wanted to drop the class into any grid contain form and it should sort accordingly.
Please guide me.
Thanks
Code:
Local loform As Form
Use (Home(2) + "Northwind\Customers")
loform = Createobject("Form")
loform.Caption = "Example grid with order by column"
loform.AddObject("MyGrid","MyGridOrd")
loform.mygrid.order_enabled = .T.
loform.mygrid.order_all()
loform.mygrid.RecordSource = "Customers"
loform.mygrid.Width = loform.Width
loform.mygrid.Height = loform.Height
loform.mygrid.Anchor = 15
loform.mygrid.Visible = .T.
loform.WindowState = 2
loform.mygrid.AutoFit()
loform.Show(1)
Define Class mygridord As Grid
*-- Current pointer to the Header object
Header = .F.
HeaderHeight = 25
*-- Enables the order column
order_enabled = .F.
Name = "MyGridOrd"
Procedure order_column
Lparameters tocolumn, tcfield
Local tccaption, tlwordwrap
Do Case
Case Pemstatus(tocolumn,"Header1",5)
tccaption = tocolumn.header1.Caption
tlwordwrap = tocolumn.header1.WordWrap
tocolumn.RemoveObject('Header1')
Case Pemstatus(tocolumn,"MyHeader",5)
tccaption = tocolumn.myheader.Caption
tlwordwrap = tocolumn.myheader.WordWrap
tocolumn.RemoveObject('MyHeader')
Endcase
tocolumn.AddObject('MyHeader', 'MyHeaderOrd', tcfield)
tocolumn.myheader.Caption = tccaption
tocolumn.myheader.WordWrap = tlwordwrap
Endproc
Procedure order_all
Local lo, lc
For Each lo In This.Columns
*-- Do not sort the columns that have some value in the TAG property
If Empty(lo.Tag)
lc = Substr(lo.ControlSource,At(".", lo.ControlSource) + 1)
*-- Remove special symbols that may appear in the ControlSource
lc = Chrtran(lc, ["'+-/*().,;], [])
This.order_column(lo, lc)
Endif
Endfor
Endproc
Procedure Init
DoDefault()
If This.order_enabled
This.order_all()
Endif
Endproc
Enddefine
*------------------------------------------------------
* Column y Header Class to sort the columns of the grid MyGridOrd
*------------------------------------------------------
Define Class mycolumnord As Column
*-- Nothing yet.
Enddefine
Define Class myheaderord As Header
FontSize = 8
FontBold = .T.
Alignment = 2
nnoreg = 0
cfield = ""
norder = 0
cfieldtype = "U"
lcyclic = .F. && The order goes from ASC > DESC > NO ORD > ASC > ETC ...
Procedure Init(tcfield)
Local ln1, ln2
This.cfield = Upper(tcfield)
ln1 = Afields(lafields, This.Parent.Parent.RecordSource)
If ln1 > 0
ln2 = Ascan(lafields, This.cfield, -1, -1, 1, 11)
If ln2 > 0
This.cfieldtype = lafields(ln2, 2)
Endif
Endif
If Not Inlist(This.cfieldtype, "U", "G", "M", "W")
This.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\DELETE.BMP", "BMP")
This.MousePointer = 15 && Hand
Endif
Endproc
Procedure Click
If Inlist(This.cfieldtype, "U", "G", "M", "W")
*- Unable to sort these types of fields.
Return
Endif
This.nnoreg = Min(Reccount(This.Parent.Parent.RecordSource), ;
RECNO(This.Parent.Parent.RecordSource))
If Vartype(This.Parent.Parent.Header) == "O" And !Isnull(This.Parent.Parent.Header)
This.Parent.Parent.Header.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\DELETE.BMP", "BMP")
If This.Parent.Parent.Header.cfield <> This.cfield
This.Parent.Parent.Header.norder = 0
Endif
Endif
Do Case
Case This.norder = 0
*-- Without order, goes to ASCending
If Ataginfo(latag,"",This.Parent.Parent.RecordSource) > 0 And Ascan(latag,This.cfield,-1,-1,1,1) > 0
*-- There is a TAG
Else
Local lcsetsafety
lcsetsafety = Set("Safety")
Set Safety Off
Select (This.Parent.Parent.RecordSource)
Execscript( "INDEX ON " + This.cfield + " TO " + This.cfield + " ADDITIVE")
Set Safety &lcsetsafety
Endif
Execscript("SET ORDER TO " + This.cfield + " IN " + This.Parent.Parent.RecordSource + " ASCENDING")
This.Parent.Parent.Header = This
This.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\SORTASC.BMP", "BMP")
Case This.norder = 1
*-- Order ASC, goes to DESCending
If Ataginfo(latag,"",This.Parent.Parent.RecordSource) > 0 And Ascan(latag,This.cfield,-1,-1,1,1) > 0
*-- There is a TAG
Else
Local lcsetsafety
lcsetsafety = Set("Safety")
Set Safety Off
Select (This.Parent.Parent.RecordSource)
Execscript( "INDEX ON " + tcfield + " TO " + This.cfield + " ADDITIVE")
Set Safety &lcsetsafety
Endif
Execscript("SET ORDER TO " + This.cfield + " IN " + This.Parent.Parent.RecordSource + " DESCENDING")
This.Parent.Parent.Header = This
This.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\SORTDES.BMP", "BMP")
Case This.norder = 2 And This.lcyclic
*-- Order DESC, goes to No Order
Execscript("SET ORDER TO 0 IN " + This.Parent.Parent.RecordSource)
This.Parent.Parent.Header = This
This.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\DELETE.BMP", "BMP")
Endcase
This.norder = Mod(This.norder + 1, Iif(This.lcyclic,3,2))
This.Parent.Parent.Refresh()
If This.nnoreg > 0
Go (This.nnoreg) In (This.Parent.Parent.RecordSource)
Endif
Endproc
Procedure RightClick
If Vartype(This.Parent.Parent.Header) <> "O"
*-- No order yet
Return
Endif
*-- With RightClick remove any order
This.nnoreg = Min(Reccount(This.Parent.Parent.RecordSource), ;
RECNO(This.Parent.Parent.RecordSource))
Execscript("SET ORDER TO 0 IN " + This.Parent.Parent.RecordSource)
This.Parent.Parent.Header.norder = 0
This.Parent.Parent.Header.Picture = Locfile(Home(4) + "Bitmaps\Tlbr_w95\DELETE.BMP", "BMP")
This.Parent.Parent.Header = This
This.Parent.Parent.Refresh()
If This.nnoreg > 0
Go (This.nnoreg) In (This.Parent.Parent.RecordSource)
Endif
Endproc
Enddefine