Select Group

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jwtea
Posts: 66
Joined: Fri Mar 23, 2018 2:01 am

Select Group

Post by jwtea » Wed May 02, 2018 9:27 am

Hello,

Currently i got 10 groups and what i want to do is that if i click on the 1st group i will receive value 1 and if
i click on 3rd group i will receive value 3 and if i click on 4th group i will receive value 4.

And i want to store the value into a variable.

As my group is created by looping so i have quite alot of group ~ ~

Anyone know the solution for this?

Thanks!!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Select Group

Post by jmburnod » Wed May 02, 2018 12:26 pm

Hi jwtea,
several ways, the best depends your context
1. A function
getting the layer for each group
sort it by layer ascending
first line = first group etc...

2. Using name of groups when you create them, something like "myGroup_1,myGroup_2, etc..." and geting the string after "_" when you click one group

Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select Group

Post by dunbarx » Wed May 02, 2018 7:34 pm

I like Jean-Marc's second idea.

It would be easy to do because when you create a new control, that control is the "last" control.

However, as of v6, the keyword "last" is not stable for groups.

I know.

So has anyone learned that this peccadillo has been fixed in v. 8/9? I just did a short test, and it seems to work.

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Select Group

Post by jacque » Wed May 02, 2018 7:55 pm

2. Using name of groups when you create them, something like "myGroup_1,myGroup_2, etc..." and geting the string after "_" when you click one group
I use this method often, and I used to name the groups as a single word with a trailing number. But a tip on the forums some years ago made me change my method -- I separate the name and the number with a space:

myGroup 1
myGroup 2
myGroup 3

This makes is very easy to extract the number. It will be "the last word of the short name of the target".

Another way to track the number of the group is to assign a custom property to each group. The property (I'll call it "cNum") will contain just the number you want to retrieve. Then you can "get the cNum of the target" to find out its numerical reference.

Using "the target" may not be appropriate, it depends on how your scripts are set up. You may need "the owner of the target" or something else.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Select Group

Post by bogs » Thu May 03, 2018 12:00 am

jacque wrote:
Wed May 02, 2018 7:55 pm
But a tip on the forums some years ago made me change my method -- I separate the name and the number with a space:
. myGroup 1
. myGroup 2
. myGroup 3
This makes is very easy to extract the number. It will be "the last word of the short name of the target".
When you were using the previous method, say, myGroup1, wouldn't "the last character of the short name of the target" suffice to extract the number easily?
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Select Group

Post by jacque » Thu May 03, 2018 12:29 am

bogs wrote:
Thu May 03, 2018 12:00 am
When you were using the previous method, say, myGroup1, wouldn't "the last character of the short name of the target" suffice to extract the number easily?
Yes, up until 10. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select Group

Post by dunbarx » Thu May 03, 2018 1:24 am

If I have to go past 10 in any situation, I just say "many".

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Select Group

Post by bogs » Thu May 03, 2018 1:42 pm

Ahhhh, I see. That makes more sense.
Image

jwtea
Posts: 66
Joined: Fri Mar 23, 2018 2:01 am

Re: Select Group

Post by jwtea » Fri May 04, 2018 3:45 am

Hello thanks all for your replies!!

But how can i create different name for each group as i'm using loop code to create them??

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select Group

Post by dunbarx » Fri May 04, 2018 4:38 am

As above, in your loop, set the name of the "last" group to some string && the number of groups:

Code: Select all

set the name of last group to "myGroup" && the number of groups
Now this may require some filtering, especially if other groups are created in between runs of your loop. Just be careful there.

Craig
Last edited by dunbarx on Fri May 04, 2018 5:43 am, edited 1 time in total.

jwtea
Posts: 66
Joined: Fri Mar 23, 2018 2:01 am

Re: Select Group

Post by jwtea » Fri May 04, 2018 5:02 am

Thanks Craig!!!


By the way,
let's say i got 10 groups right and inside each group got a unique number field and a widget.

So if i click on the 1st group widget, the unique number field value inside the 1st group will be put into a
field call holding field.

And if i click on the 3rd group widget, the unique number field value inside the 3rd group will be put into the same field call holding field.

Can i know what the code for the above function??

Thanks Craig, appreciated alot!!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select Group

Post by dunbarx » Fri May 04, 2018 6:00 am

Hmmm.

Are your 10 groups and their children named in order? Or do they possess a common property? You really need a way to discriminate among them. So you could name your groups after animals or fruit. And then you also have to associate the widgets and unique number field in like kind. This preparation is critical to writing code that can make sense later on, and keep that code itself simple and readable. So let us assume that all three of these controls (widget, uniqueNumberField and group) all have a common property. Let us say they are all numbered like Jacque mentioned:

group "myGroup 4"
widget "myWidget 4"
field "uniqueNumberField 4"

In the card script:

Code: Select all

on mouseUp
put word 2 of the short name of the target into tNumber
put field "uniqueNumberField" && tNumber into field "holding field"
end mouseUp
So, again, have you ordered your groups and the controls of interest inside them in a fashion that allows them to fall into "natural" sets with common properties? If not, do so now, and always do so in the future.

Write back if none of this makes sense.

Craig

jwtea
Posts: 66
Joined: Fri Mar 23, 2018 2:01 am

Re: Select Group

Post by jwtea » Fri May 04, 2018 6:57 am

Hello Craig, the reason why all my group and field got same property because my code is written as below.

Below is my preopencard script

PS: tArray is fetching my json object

Code: Select all

 put 0 into myVar
         put 1 into myID
         put 1 into myID1
         put 156 into myPosition1
         put 184 into MyPosition2
         repeat until tArray["users"][myID1]["email"] is empty
            
            add 1 to myVar
            add 1 to myID
            add 1 to myID1
            add 0 to myPosition1
            add 164 to myPosition2
            
            if tArray["users"][myID1]["email"] is not empty then
               
               copy group "usersbox"
               paste group
               move group "usersbox" to myPosition1 , myPosition2 in timing second
               
               put tArray["users"][myID]["idnumber"] into field "idnumber" 
               put tArray["users"][myID]["fullname"] into field "name" 
               put tArray["users"][myID]["email"] into field "email" 
               put tArray["users"][myID]["role"]["rolename"] into field "rolename" 
               
            end if
         end repeat



PS: I had tried to use your code but it only copy the last group's unique number field ~ ~

Klaus
Posts: 13793
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Select Group

Post by Klaus » Fri May 04, 2018 11:01 am

Hi jwtea,

just change these lines:

Code: Select all

...
 if tArray["users"][myID1]["email"] is not empty then
    ## copy group "usersbox"
    ## paste group
    clone group "usersbox"
    set the name of IT to ("userbox" && myVar)
    move group ("usersbox" && myVar) to myPosition1 , myPosition2 in timing second
...
Just to be sure, you are NOT trying to fill the fields of the just created group with the next lines?
That will not work of course.

Code: Select all

...
put tArray["users"][myID]["idnumber"] into field "idnumber" 
put tArray["users"][myID]["fullname"] into field "name" 
put tArray["users"][myID]["email"] into field "email" 
put tArray["users"][myID]["role"]["rolename"] into field "rolename" 
...
Best

Klaus

jwtea
Posts: 66
Joined: Fri Mar 23, 2018 2:01 am

Re: Select Group

Post by jwtea » Fri May 04, 2018 11:24 am

Hello Klaus , thanks for your help! Now my group got each and special unique name.

But i still can't get this to work after typing the code provided by Craig. Any idea why??

Code: Select all

on mouseUp
put word 2 of the short name of the target into tNumber
put field "uniqueNumberField" && tNumber into field "holding field"
end mouseUp

dunbarx wrote:
Fri May 04, 2018 4:38 am
By the way,
let's say i got 10 groups right and inside each group got a unique number field and a widget.

So if i click on the 1st group widget, the unique number field value inside the 1st group will be put into a
field call holding field.

And if i click on the 3rd group widget, the unique number field value inside the 3rd group will be put into the same field call holding field.

Can i know what the code for the above function??

Post Reply

Return to “Talking LiveCode”