Collapse and expand Tree View Widget from code

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

Re: Collapse and expand Tree View Widget from code

Post by bwmilby » Thu Sep 26, 2019 12:55 pm

That is not possible as a single command in the current implementation. You can get the fold state array and then create one that will have everything expanded. Since the widget constructs the fold state array in a lazy manner (only adds keys as they are visited and unfolded) and the entire tree is not parsed initially depending on size, adding it to the widget will take a bit of code. Probably a good Hacktoberfest addition.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

PeirceEd
Posts: 2
Joined: Fri Mar 15, 2013 7:09 pm

Re: Collapse and expand Tree View Widget from code

Post by PeirceEd » Sun Feb 05, 2023 12:42 am

Here is one solution for expanding a tree completely, up to six levels. There may be a way to do it recursively but this solution has the advantage of avoiding a headache.

Code: Select all

on unfoldEntireTree pWidgetID,pCardNumber,pStackName
   lock screen
   local pFoldArray,tK1s,tK2s,tK3s,tK4s,tK5s,tK6s,tK1,tK2,tK3,tK4,tK5,tK6,tL,pData
   put the arrayData of widget id pWidgetID of cd pCardNumber of stack pStackName into pData
   repeat for each key tKey1 in pData
      put false into pFoldArray[tKey1]["folded"]
      put the keys of pData[tKey1] into tK1s
      repeat for each line tK1 in tK1s
         if pData[tKey1][tK1] is not an array then 
            next repeat
         else 
            put false into pFoldArray[tKey1]["array"][tK1]["folded"]
            put the keys of pData[tKey1][tK1] into tK2s
            repeat for each line tK2 in tK2s
               get  pData[tKey1][tK1][tK2]
               if it is not an array then next repeat
               else
                  put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["folded"]
                  put the keys of pData[tKey1][tK1][tK2] into tK3s
                  repeat for each line tK3 in tK3s
                     get  pData[tKey1][tK1][tK2][tK3]
                     if it is not an array then next repeat
                     else
                        put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["folded"]
                        put the keys of pData[tKey1][tK1][tK2][tK3] into tK4s
                        repeat for each line tK4 in tK4s
                           get  pData[tKey1][tK1][tK2][tK3][tK4]
                           if it is not an array then next repeat
                           else
                              put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["folded"]
                              put the keys of pData[tKey1][tK1][tK2][tK3][tK4] into tK5s
                              repeat for each line tK5 in tK5s
                                 get  pData[tKey1][tK1][tK2][tK3][tK4][tK5]
                                 if it is not an array then next repeat
                                 else
                                    put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["array"][tK5]["folded"]
                                    put the keys of pData[tKey1][tK1][tK2][tK3][tK4][tK5] into tK6s
                                    repeat for each line tK6 in tK6s
                                       get  pData[tKey1][tK1][tK2][tK3][tK4][tK5][tK6]
                                       if it is not an array then next repeat
                                       else
                                          put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["array"][tK5]["array"][tK6]["folded"]
                                       end if
                                    end repeat
                                 end if
                              end repeat
                           end if
                        end repeat
                     end if
                  end repeat
               end if
            end repeat
         end if
      end repeat
   end repeat
   set the foldState of widget id pWidgetID of cd pCardNumber of stack pStackName to pFoldArray
end unfoldEntireTree

PeirceEd
Posts: 2
Joined: Fri Mar 15, 2013 7:09 pm

Re: Collapse and expand Tree View Widget from code

Post by PeirceEd » Sun Feb 05, 2023 1:06 am

Or here's the same solution but much shorter and faster, come to think of it:

Code: Select all

on expandEntireTree pWidgetID,pCardNumber,pStackName
   lock screen
   local pFoldArray,tK1s,tK2s,tK3s,tK4s,tK5s,tK6s,tK1,tK2,tK3,tK4,tK5,tK6,tL,pData
   set the foldState of widget id pWidgetID of cd pCardNumber of stack pStackName to empty
   put the arrayData of widget id pWidgetID of cd pCardNumber of stack pStackName into pData
   repeat for each key tKey1 in pData
      put false into pFoldArray[tKey1]["folded"]
      repeat for each key tK1 in pData[tKey1]
         if pData[tKey1][tK1] is not an array then 
            next repeat
         else 
            put false into pFoldArray[tKey1]["array"][tK1]["folded"]
            repeat for each key tK2 in pData[tKey1][tK1]
               if pData[tKey1][tK1][tK2] is not an array then 
                  next repeat
               else 
                  put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["folded"]
                  repeat for each key tK3 in pData[tKey1][tK1][tK2]
                     if pData[tKey1][tK1][tK2][tK3] is not an array then 
                        next repeat
                     else 
                        put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["folded"]
                        repeat for each key tK4 in pData[tKey1][tK1][tK2][tK3]
                           if pData[tKey1][tK1][tK2][tK3][tK4] is not an array then 
                              next repeat
                           else 
                              put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["folded"]
                              repeat for each key tK5 in pData[tKey1][tK1][tK2][tK3][tK4]
                                 if pData[tKey1][tK1][tK2][tK3][tK4][tK5] is not an array then 
                                    next repeat
                                 else 
                                    put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["array"][tK5]["folded"]
                                    repeat for each key tK6 in pData[tKey1][tK1][tK2][tK3][tK4][tK5]
                                       if pData[tKey1][tK1][tK2][tK3][tK4][tK5][tK6] is not an array then 
                                          next repeat
                                       else 
                                          put false into pFoldArray[tKey1]["array"][tK1]["array"][tK2]["array"][tK3]["array"][tK4]["array"][tK5]["array"][tK6]["folded"]
                                       end if
                                    end repeat
                                 end if
                              end repeat
                           end if
                        end repeat
                     end if
                  end repeat
               end if
            end repeat
         end if
      end repeat
   end repeat
   set the foldState of widget id pWidgetID of cd pCardNumber of stack pStackName to pFoldArray
end expandEntireTree

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Collapse and expand Tree View Widget from code

Post by stam » Sun Feb 05, 2023 3:13 pm

TL;DR - but if you're looking to collapse all, this has worked for me in the past, but admittedly this is only for a tree widget with 1 level, i.e. no sub-arrays:

Code: Select all

 set the foldState of widget "<tree widget name>" to false
to expand all:

Code: Select all

command expandAll pTree //long id of the tree widget
   local tNewState
   get the keys of (the arrayData of pTree)
   repeat for each line tLine in it
      put false into tNewState[tLine]["folded"]
   end repeat
   set the foldState of pTree to tNewState
end expandAll
Just rechecked and it does work for my purposes, hopefully this may help...
BW
S.

Post Reply

Return to “Talking LiveCode”