storing hidden id with Option Menu

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
PureMoxie
Posts: 7
Joined: Mon Feb 22, 2021 5:00 pm

storing hidden id with Option Menu

Post by PureMoxie » Mon Feb 22, 2021 5:14 pm

Greetings,

I am coming from a web development background. In html, the selected value of an option menu can be different than the displayed text for the menu item:

Code: Select all

<option value="1">Red</option>
Let's say I have a db table products with a column called color_id that references another table called colors. I want to store the id of the color in products, not the name. If I have colors Red, Blue, and Green and later I want to change the color names to Apple, Sky, and Grass, storing the color_id in products instead of the text allows this change without any updates to the products table.

What's a good approach to accomplish this in LC with an Option Menu button? It seems like the only way is to create a hidden field that I update with the matching color id value when the option menu changes. That makes sense and isn't onerous, I just wanted to make sure I wasn't missing something obvious.

stam
Posts: 3070
Joined: Sun Jun 04, 2006 9:39 pm

Re: storing hidden id with Option Menu

Post by stam » Mon Feb 22, 2021 5:49 pm

Hi PureMoxie

If i understood correctly you want to have a 'row tag' property associated with an line in the option menu? While you could list them in a field separately, i would have thought a much better way to do this would be with a custom property of the menu button?

You could sent this up either as a CR-delimited list so each line of the custom prop corresponds to a line in the option menu, or as a numbered array which might be better as you can have the base-key reflecting the row and have additional tags (eg localisations) with each of these.
eg for an custom property of the option menu button called 'rowID' you could have rowID[1]['name'], rowID[1]['color'] etc - and you can script the menu so it it loads it's text from the custom property array.

I'm sure there are other ways - and others will chime in!

PureMoxie
Posts: 7
Joined: Mon Feb 22, 2021 5:00 pm

Re: storing hidden id with Option Menu

Post by PureMoxie » Mon Feb 22, 2021 6:14 pm

Ah, glad I asked. I hadn't gotten to custom properties yet, but this indeed looks like the way to go. Thanks!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: storing hidden id with Option Menu

Post by dunbarx » Mon Feb 22, 2021 8:07 pm

You are going to LOVE custom properties. They survive sessions, and can contain any and all types of data.

Craig

PureMoxie
Posts: 7
Joined: Mon Feb 22, 2021 5:00 pm

Re: storing hidden id with Option Menu

Post by PureMoxie » Tue Feb 23, 2021 4:31 pm

Okay, so I now have populated a custom property for the Option Menu from my database query with the row id and the option text, for example cOptions[1] = 'option1' and cOptions[2] = 'option2'.

Now my question is if it is possible to retrieve the key from a given value so that if the selected menu option is 'option1' I can get key 1 returned (literally "1" not the value of key 1)? I can't find something like a keyByValue function, so do I need to reverse my keys/values in the first place so that it is like cOptions['option1'] = 1?

Klaus
Posts: 14192
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: storing hidden id with Option Menu

Post by Klaus » Tue Feb 23, 2021 5:29 pm

Hi PureMoxie,

depends on how the data in the custom property is "organized"?

Example, in the custom prop you could store a pair of "Text in menu" and VALUE, separated with a TAB or whatever
option1 TAB 1
option2 TAB 2
etc...
Then you could just do a lineoffset and extract the first item of that line.
Example script for option menu button:

Code: Select all

on menupick tMenuItem
  put the cLookUp of btn "your option menu button" into tData
  put lineoffset(tMenuItem & TAB,tData) into tLine
  if tLine = 0 then
    answer "Grand inconvenience, no matching line found!"
    exit to top
  end if
  answer "Appropriate value for this menuitem -> " && item 1 of line tLine of tData
end menupick
You get the picture.


Best

Klaus

PureMoxie
Posts: 7
Joined: Mon Feb 22, 2021 5:00 pm

Re: storing hidden id with Option Menu

Post by PureMoxie » Tue Feb 23, 2021 5:47 pm

Thanks, Klaus, makes sense!

I also figured out a little function to return an array's key when passed an array and a value. It assumes unique values (it would only return the first match otherwise), but it does what I need in this case.

Code: Select all

function keyByValue pArray pValue
   repeat for each line tKey in the keys of pArray
      if pArray[tKey] = pValue then
         return tKey
      end if
   end repeat
end keyByValue

Klaus
Posts: 14192
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: storing hidden id with Option Menu

Post by Klaus » Tue Feb 23, 2021 6:05 pm

Yep, whatever does the job and fits in the current situation. :-)

I forgot an important line in my script:

Code: Select all

on menupick tMenuItem
  put the cLookUp of btn "your option menu button" into tData
  put lineoffset(tMenuItem & TAB,tData) into tLine
  if tLine = 0 then
    answer "Grand inconvenience, no matching line found!"
    exit to top
  end if

  ## !!!
  set itemdel to TAB
  ## !!!
answer "Appropriate value for this menuitem -> " && item 1 of line tLine of tData
end menupick

jiml
Posts: 339
Joined: Sat Dec 09, 2006 1:27 am

Re: storing hidden id with Option Menu

Post by jiml » Tue Feb 23, 2021 9:57 pm

Pulldown and Popup Menu Buttons can have TAGs in LiveCode.
Tags are placed after the "|" character and after an accelerator which follows a "/" character.

So, just put this into a pulldown menu button:
Choice 1/|monkey business
Choice 2/|COVID-19
Choice 3/|LiveCode


The user will see:
Choice 1
Choice 2
Choice 3

Code: Select all

on menuPick pChosenItem
   put pChosenItem
end menuPick
will return the TAG for the selected menuItem rather than the menuItem itself.
So, choosing "Choice 2" will return COVID-19

This does not work with Option Menu Buttons. Perhaps you can switch button types.

Jim Lambert

stam
Posts: 3070
Joined: Sun Jun 04, 2006 9:39 pm

Re: storing hidden id with Option Menu

Post by stam » Wed Feb 24, 2021 12:23 am

jiml wrote:
Tue Feb 23, 2021 9:57 pm
Pulldown and Popup Menu Buttons can have TAGs in LiveCode.
Tags are placed after the "|" character and after an accelerator which follows a "/" character.
...
will return the TAG for the selected menuItem rather than the menuItem itself.
Wow thanks Jim, that's a cool feature i must have missed completely. Haven't seen that documented anywhere... this forum is a fountain of knowledge, absolute treasure trove :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: storing hidden id with Option Menu

Post by dunbarx » Wed Feb 24, 2021 7:28 am

This does not work with Option Menu Buttons. Perhaps you can switch button types.
It also does not work with comboBoxes. Something about the complex nature of those two styles that precludes that feature.

Another cool feature is submenus, created by placing tab chars in the line following the "main" menuItem. Check out the User guide on page 176.

Craig

Post Reply