xBrowse Sort a Tree by a column n on oItem:Cargo

xBrowse Sort a Tree by a column n on oItem:Cargo

Postby reinaldocrespo » Tue Jan 28, 2020 9:08 pm

Hello everyone;

I'm looking to sort an xbrowse displaying a tree. Sorting should be by cargo contents [n] on Tree item branch. For example: suppose I wish to sort this tree by oItem:Cargo[ 2 ] and then refresh the xbrowse showing the newly ordered tree.

Code: Select all  Expand view  RUN

   ::oTree := NIL
   TREE ::oTree                           //::oTree is type Tlinklist

   FOR i := 1 to 10

      oItem := ::oTree:Add( "Item #"+cValToChar( i )  )

      oItem:Cargo := { i, hb_RandomInt(), Time() }
      oItem:nLevel  := 1
      oItem:bAction := { |o| o:SetTree( ::SubTree( o ) ), o:bAction := Nil }

      EVAL( oItem:bAction, oItem )
     
   NEXT i

   ENDTREE
 


Can someone suggest how to sort the tree so that it is ordered by that random integer?

Thank you,


Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse Sort a Tree by a column n on oItem:Cargo

Postby reinaldocrespo » Wed Jan 29, 2020 10:59 pm

After a while without any suggestions, I'm thinking perhaps I did not explain my question well enough. Suppose I want to order this tree below by the 2nd column of each parent branch (that branch is the state name with folder bmp). This sample code is from xbrwTree.prg on the Samples folder. Notice I only changed a line to include a 2nd column on each parent branch with a random number between 0 and 1000.

Code: Select all  Expand view  RUN

#include "FiveWin.ch"
#include "xbrowse.ch"

function Main()  

   local oWnd, oBrw

   USE Customer
   INDEX ON Field->State TO State
   SET ORDER TO "State"
   GO TOP
   
   DEFINE WINDOW oWnd TITLE "DBF shown as a tree with XBrowse"

   @ 0, 0 XBROWSE oBrw OF oWnd LINES CELL

   oBrw:SetTree( BuildTree(), { "open", "close", "go" } )

   ADD TO oBrw DATA oBrw:oTreeItem:Cargo[ 1 ] HEADER "Last"
   ADD TO oBrw DATA oBrw:oTreeItem:Cargo[ 2 ] HEADER "First"

   oBrw:nMarqueeStyle = MARQSTYLE_HIGHLROW

   oBrw:CreateFromCode()
   oBrw:aCols[ 1 ]:cHeader = "State & City"

   oWnd:oClient = oBrw
   
   ACTIVATE WINDOW oWnd
   
return nil  

static function BuildTree()

   local oTree, cState

   TREE oTree
      while ! Eof()
         if Empty( cState )
            _TreeItem( Customer->State ):Cargo := { hb_randomInt(1000), Space( 20 ) }  //<<---- changed this line
            TREE
            cState = Customer->State
         else
            if cState != Customer->State
               ENDTREE
               cState = Customer->State
               _TreeItem( Customer->State ):Cargo := { hb_randomInt(1000), Space( 20 ) }   //<<---- and this line
               TREE
            endif  
         endif  
         if Customer->State == cState
            _TreeItem( Customer->City ):Cargo := { Customer->Last, Customer->First }
         endif  
         SKIP
      enddo
      ENDTREE
   ENDTREE

   GO TOP

return oTree
 


Is there a way to order this tree by the random number on column #2? The child branches would have to stay attached to its original parent branch. Any suggestions?

Thank you,


Reinaldo
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse Sort a Tree by a column n on oItem:Cargo

Postby Marcelo Via Giglio » Thu Jan 30, 2020 2:06 pm

Hola Reinaldo,

I think, you need to create other tree in base to the original, let me see the tree class for a easy solution

regards

Marcelo
Marcelo Via Giglio
 
Posts: 1065
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia

Re: xBrowse Sort a Tree by a column n on oItem:Cargo

Postby reinaldocrespo » Thu Jan 30, 2020 4:47 pm

Hola Marcelo;

¿Así que sigas vivo? Supongo que así dirás de mi. :-)

Si esto es algo que no existe como sospecho entonces se me ocurre extender la clase TLinkList para añadir un sort por alguna otra columna que no sea necesariamente la primera (cPrompt) --y creo que esto es lo sugieres. ¿Correcto?

Me voy a dar la tarea y comparto la solución aquí.

Gracias,

Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse Sort a Tree by a column n on oItem:Cargo

Postby reinaldocrespo » Thu Jan 30, 2020 11:12 pm

Hello everyone;

Here is the method I wrote to sort my xbrowse tree by any column. It has been tested and it works really nice.

Code: Select all  Expand view  RUN

METHOD Sort( nCol ) CLASS TBatchPayments
   LOCAL oItem    := ::oTree:oFirst
   LOCAL oPrev    := oItem:oPrev
   LOCAL oNext    := ::oTree:oLast:oNext
   LOCAL aItems   := {}
   LOCAL n

   IF nCol == NIL .OR. nCol > LEN( oItem:Cargo ) .OR. --nCol < 1   ;RETURN NIL ;ENDIF

   WHILE oItem != nil

      IF oItem:nLevel == 1
         AADD( aItems, oItem )
      ENDIF

      oItem = oItem:oNext

   END

   ASort( aItems,,, { |x, y| x:Cargo[ nCol ] < y:Cargo[ nCol ]} )

   ::oTree:oFirst       := aItems[ 1 ]
   ::oTree:oFirst:oPrev := oPrev

   for n := 2 to Len( aItems )
      aItems[ n ]:oPrev    := aItems[ n - 1 ]
      aItems[ n - 1 ]:SetNext( aItems[ n ] )
   next n

   ::oTree:oLast := ATail( aItems )
   ::oTree:oLast:SetNext( oNext )

RETURN Nil
 



Reinaldo.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 69 guests