Getting array data from a Tree View widget

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Getting array data from a Tree View widget

Post by M-A Kuttner » Tue Nov 29, 2022 4:00 am

Hi all. Arrays have always been my Waterloo, so I've avoided them by faking it with table fields and whatnot. However, I now have to use them because I'm doing a project that uses the Tree View widget.

I've looked around and gleaned what I can from the online Livecode lessons, but I need a bit more help. What I'm trying to do is get a specific value from the ArrayData and then turn that data into something I can actually place into a field. I'm assuming something like this:

Code: Select all

get the hilitedElement of widget Tree1
--?? Somehow use this as a key to return the value associated with it ??
put the returnedValue into tArray
put (arrayEncode(tArray)) into field "ReturnedValue"
I figured out how to get all the ArrayData from the widget, but when I turn it into placeable data using arrayEncode, it returns all the keys and values in the array with no delimiters in between. I'm definitely over my head on this one, so any help is greatly appreciated.

Thanks!
M-A
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

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

Re: Getting array data from a Tree View widget

Post by stam » Tue Nov 29, 2022 9:04 am

Not sure I’d use arrayEncode.
I did tinker with tree widgets some time ago and don’t think I used that at all.

From memory, the widgets handler returns the keys that contain the value you want to get at, but it’s been a while since I’ve used a tree widget (for me they’re mostly just helpful for working with array data in the ide as you can just dump an array into them.

Unfortunately I’m at work and can’t post an example, but don’t think it’s that hard and will do so later if no one else has…

But do you really need to use a tree widget?

S.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Getting array data from a Tree View widget

Post by FourthWorld » Tue Nov 29, 2022 9:31 am

Some background on arrays:
https://forums.livecode.com/viewtopic.p ... 07#p202907

How many dimensions is your array?

If it's just one you can use the combine command to turn the array structure into a string suitable for display in a field.

If it's more than one we'll want to know more about the goal of the field display. What needs to be shown there that isn't shown in the tree widget, and how will it be used?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Getting array data from a Tree View widget

Post by stam » Tue Nov 29, 2022 1:20 pm

Dear M-A,

If I understand correctly you want to get at the value (not the key) of what you're clicking on. Please see and example stack (can be modified if you want to show the last key instead, but I'll leave that as an exercise to you ;) ). I on purpose have included a separator in the tree widget of this stack to make it clear what is a key and what is a value, as it can be misleading.

There might be a better way of doing this - but what I'm doing is building a string with the array keys (array[lvl1][lvl2][lvl3] for example) and making it into a command by putting in into a do merge() (note that the contents of do merge should all be text; if an expression needs to be evaluated inside this (eg 1+1) then enclose this in double square brackets).
tree widget example.livecode.zip
(2.05 KiB) Downloaded 72 times

I also attach a stack which I created for a completely different discussion but shows how to do some other manipulations with the tree widget, like collapse/expand all etc. I haven't looked at the code in this in a long time, but since it works, I attach this as well..
sortArrayExample.livecode.zip
(2.73 KiB) Downloaded 75 times

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Re: Getting array data from a Tree View widget

Post by M-A Kuttner » Tue Nov 29, 2022 8:04 pm

Thanks for the resources and help, all! I'll go through this and report back what I find. 8)
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Getting array data from a Tree View widget

Post by mrcoollion » Tue Nov 29, 2022 9:43 pm

To get the tree and last element info you could use the following script (if this is what you need?).
Place it in the TreeView and have a field in your card named "ElementData".

Code: Select all

   on hiliteChanged 
   put the hilitedElement of me into tElement
   if the last character of tElement is "," then delete the last character of tElement
   put tElement into tArrayParts
   put tElement into fld"ElementData" 
   end hiliteChanged
Regards,

Paul

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”