<< back to BigData
Using Drop Down Menus - Populating them
Example 1- Lookup Median Age and Income in Counties of Maryland
1. Unknown Choices - Make a Pull Down List
I am not a great typist and would not like to type unless I need to. It would be nice to pick the counties from a list instead of typing each one in the Look Up field.
Besides, I do not know the names of all the counties. I do not always know what to type. Why can't we use a list where I can pick from all the names?
We want it to look and work like this::
Lets do it:
1 We start with this:
2. Delete the text entry field and drag over a Pull Down Menu object, name it "counties" , make the label say "Pick a County"
3. Add the following code to build a list of counties for us:
repeat for each line x in temp // go thru the list
put item 1 of x & return after myList // add the county to the variable myList and go to next line
end repeat // myList should contain all the counties now
set the text of button "counties" to myList // put them on the button
--> Which we add to our "Load Data" button (which may look something like this...)
answer file "Pick a file to input"
put url ("file:" & myfile) into temp
repeat for each line x in temp
put item 1 of x & ", " & item 8 of x & ", " & char 2 to 9 of item 10 of x & return after field "x"
repeat for each line x in temp
put item 1 of x & return after myList
set the text of button "counties" to myList
4. Copy the script from your "Find" button
put field "mycounty" into theCounty
find theCounty in field "x"
if the result is empty then
put word 2 of the foundLine into theNum
put item 1 of line theNum of field "x" into field "a"
put item 2 of line theNum of field "x" into field "b"
put item 3 of line theNum of field "x" into field "c"
end if
..... and paste it into the PullDown button script
on menuPick pItemName
switch pItemName
put field "mycounty" into theCounty
find theCounty in field "x"
if the result is empty then
put word 2 of the foundLine into theNum
put item 1 of line theNum of field "x" into field "a"
put item 2 of line theNum of field "x" into field "b"
put item 3 of line theNum of field "x" into field "c"
end switch
on menuPick
notes:
1) We do not need the switch statement (which is like an if statement - if it is choice-1 then do this, if it is choice 2 then do that,...)
2) We do not need to get the county from the field anymore, it is in pItemName
3) pItemName holds the user's choice, so we just do the find on it
..... and change it to the following:
delete the lines - "switch..." and "end switch" (we do not need them)
delete the line - "put field...", (we do not need it anymore, we are getting the name "pItemName" from the drop-down menu)
on menuPick pItemName
find pItemName in field "x"
if the result is empty then
put word 2 of the foundLine into theNum
put item 1 of line theNum of field "x" into field "a"
put item 2 of line theNum of field "x" into field "b"
put item 3 of line theNum of field "x" into field "c"
end if
on menuPick
5. Add the following to reset the search to the top of the list: find empty in field "x"
on menuPick pItemName
find empty in field "x"
find pItemName in field "x"
if the result is empty then
put word 2 of the foundLine into theNum
put item 1 of line theNum of field "x" into field "a"
put item 2 of line theNum of field "x" into field "b"
put item 3 of line theNum of field "x" into field "c"
end if
on menuPick
All done, it should work
Example 2- Lookup Unemployment Rates in the Different States
2. Too Many Choices - Make a Pull Down List
Suppose, you have data on all the states and want the user to be able to look up items by the state's name. Instead of having them type the state in a text field, why not just give them a dropdown menu with all the states in it, So all they have to do is pick the one they want.
the screenshot below shows the Drop Down menu with our data in a field to the right. you can see the first field or item of each line is the name of the state. We used that to populate the text of our button:
In this example, we placed a dropdown menu object on the screen named "States".
Then when we load the data, we added the following code: (either in the "load Data" button or in the "openCard" script)
repeat for each line li in x // go through each line in our data
put item 1 of li & return after t // get the name of the state, add a line return to skip to the
end repeat // ... next line and put it in a variable I called "t"
// we now have all the states names in our variable called "t"
set the text of btn "State" to t // Now, put that into the text of the button
The following code is on the "Pick a State" button:
on menuPick pItemName // pItemName is the state that the user picked
find empty in field "Data" // this resets the search to the top of the list
find pItemName in field "Data" // find the state in our data
put word 2 of the foundLine into theNum // this gets the line number of the state that was found
put item 1 of line theNum of field "Data" into field "State" // this puts the state's name on the screen
put item 10 of line theNum of field "Data" into field "Rate" // put the rate on the screen
Screenshots (with the data field hidden and some minor cosmetic changes:
Note: The data file is at the bottom for you to download and use: