Page 1 of 2

Populate Option Menu from Array [Solved]

Posted: Mon Jun 01, 2015 11:46 am
by antrax13
Hi I have an array that is coming from DB so every time it will be different.

When array is created I need to populate Option Menu button with these values.

My Code is:

Code: Select all

 repeat for each element title in tColumnTitles
         --answer title 
         put the text of btn "omLocation" to title
 end repeat
it will set the value always to last element in array and I would like to see all elements to be available/visible in Option Menu dropdown not just the last one

so IE if array contains Peter,John,Martin

I want to see all options in dropdown not just Martin

Re: Populate Option Menu from Array

Posted: Mon Jun 01, 2015 12:18 pm
by jmburnod
Hi,
What about this ?

Code: Select all

on buildMyMenu
   put "Peter,John,Martin" into tColumnTitles
   replace "," with cr in tColumnTitles
   set the text of btn "omLocation" to  tColumnTitles
end buildMyMenu
Best regards
Jean-Marc

Re: Populate Option Menu from Array

Posted: Mon Jun 01, 2015 1:15 pm
by Klaus
Hi antrax13,

1. welcome to the forum! :D

2. That depends on your Array! What format does your array have exactly?
e.g. like this:
tColumTitles[1]["Peter"]
tColumTitles[2]["John"]
tColumTitles[3]["Martin"]
...
?


Best

Klaus

Re: Populate Option Menu from Array

Posted: Mon Jun 01, 2015 1:39 pm
by antrax13

Code: Select all

 
local lTest
      repeat for each element lTitle in tColumnTitles
         put lTitle & cr after lTest
      end repeat
      set the text of btn "omLocation" to lTest
this did the trick and now all options are visible in option menu button

Re: Populate Option Menu from Array

Posted: Mon Jun 01, 2015 3:04 pm
by Klaus
Glad we could help! :D

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 3:13 pm
by CAsba
Where is the code put ?

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 3:33 pm
by jmburnod
Hi CAsba,
Where is the code put ?
set the text of...
Best
Jean-Marc

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 3:40 pm
by CAsba
Hi Jimburnod
Sorry, I don't get it. I have a csv array in a field, consisting of current year, current year less 1, less 2, etc., that I want to put as a list of choices for the user. How best to populate the menu choices space ?

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 4:14 pm
by dunbarx
CAsba.

So you do not have an "array", but just text in a field. Be careful, since "array" means something very specific in LiveCode.

Is your data comma delimited, as "csv' implies? if so, you can create a cascading menu to display the data.

Craig

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 4:38 pm
by CAsba
Hi Craig,
yes, it is comma seperated, in a field. I guess I don't know what an array is, then...
So, how would I proceed to set up a cascading, or other, drop down menu, with the values in the field as choices ?

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 4:45 pm
by dunbarx
It occurs to me, do you know how to create a hierarchal (cascading) menu? I ask because I am not sure that an option menu is a good choice. Each of the four menu buttons acts a little differently. Read about this in the User Guide, page 176.

Check out this stack, which has all four menu buttons. Play with it. If you need more submenus, try to make them, and come back here if you need help.
MenuTest.livecode.zip
(1.27 KiB) Downloaded 148 times
Craig

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 5:08 pm
by CAsba
Hi Craig,
True, I know very little about the various menu possibilities. But, I used the option type successfully to allow the user to choose the day and the month of the date required. The difference with the year is that the current year has to be one of the choices, with the other choices being current year -1, -2, -3 etc. So, it's a dynamic choice, which will change every year, unlike the day and month choices. So, instead of manually populating the option menu, as I did with day and month, it is necessary to get the current year listed as a reference for the choices.
So, the situation is, to recap, the option menu appears to offer the required facility, as shown in the month and year menus, but I need a way to insert, as choices, a list of years headed by the current year. I have read quite a lot about setting up menus but nothing to enlighten me re this issue.
Hoping you can help.

Re: Populate Option Menu from Array [Solved]

Posted: Mon Nov 07, 2022 7:42 pm
by dunbarx
Keep talking.

This is simple. The keyword "dateItems" allows full control of dates and times. One can add or subtract values from the several parts of that gadget, and obtain any other date and time. Please experiment with this until you have it down. It is not complicated and actually fun to do so.

You will soon see that one can "convert" (a command), for example, today's date to "dateItems". When you get that value, add, say, 50 to the third item. (the "day of the month"). Once you do that, convert back to the long date. You will see that all the math has been done for you and the new date, over seven weeks later, is properly formatted. With that tool you can then create the list you require.

Get going, and if you need more help, come back.

Craig

Re: Populate Option Menu from Array [Solved]

Posted: Tue Nov 08, 2022 7:12 pm
by jacque
Since you only need the year, you can get that from the date function. Either parse it out from the date or convert the date to dateitems and get item 1 of that.

After you know the current year it is easy to subtract 1 from it as many times as you need. Keep the years in a return-delimited list. When the list is ready:

Code: Select all

set the text of button "years" to tList 

Re: Populate Option Menu from Array [Solved]

Posted: Tue Nov 08, 2022 7:34 pm
by FourthWorld
This thread is conceptually similar; would it be helpful to future readers if I merged this one with that?

https://forums.livecode.com/viewtopic.php?f=7&t=37440