Dependent Scrolling List Field

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
kyoush
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5
Joined: Thu Mar 14, 2013 12:44 pm

Dependent Scrolling List Field

Post by kyoush » Mon Apr 22, 2013 11:44 am

Hi there Livecoders,

I am trying to use two Scrolling List Field, where the content of the second depends on the selection in the first. e.g. if i select A in the first field, the second field shows 1,2,3 and if i select B in the first field, 4,5,6 are displayed as options in the second.

Is there a way to put the entire list of possibilities as contents of the second field and then selectively hide certain entries?
Or do i have to create a switch-case code fragment which erases and rewrites the contents of the second field?

Thanks for the help in advance.
...Livecode newbie, finding my way... :)

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Dependent Scrolling List Field

Post by snm » Mon Apr 22, 2013 11:54 am

There are a lot of possibilities in LC. If it's my project I'll use arrays to keep data of field B "related" to data shown in field A and then display them in DataGrids or list fields.

Marek

kyoush
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5
Joined: Thu Mar 14, 2013 12:44 pm

Re: Dependent Scrolling List Field

Post by kyoush » Mon Apr 22, 2013 12:01 pm

Thanks for the tip Marek!
I'm a complete Newbie at LiveCode, but let me give that a try and check back if i run into hurdles.
...Livecode newbie, finding my way... :)

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Dependent Scrolling List Field

Post by jmburnod » Mon Apr 22, 2013 12:49 pm

Hi kyoush,

As said Marek, you have many ways to do this
Using array is the best , but it is maybe less lisible than others
I did a little stack with your exemple
Let we know if you want others ways

Best regards
Jean-Marc
Attachments
SimpleArray.livecode.zip
(1.56 KiB) Downloaded 252 times
https://alternatic.ch

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Dependent Scrolling List Field

Post by snm » Mon Apr 22, 2013 5:18 pm

Clever done. I like it Jean-Marc.
So some little upgrade of defMylistInMU handler from me:

Code: Select all

-- Mouseup fld "MyListIn"
on defMylistInMU
   put empty into fld MyListOut
   put the short name of the target into tFld
   put (the hilitedlines of fld tFld) into t
   repeat for each item i in t
      put item 1 of line i of fld tFld into tCat
      put gMyArray[tCat] & cr after fld MyListOut
   end repeat
   delete last char of fld MyListOut
end defMylistInMU
Then if you set multipleHilite, nonContiguousHilite and toggleHilites properties of fld MyListIn, you can choose any quantity of "categories" and list all values of them.

Marek

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Dependent Scrolling List Field

Post by jmburnod » Mon Apr 22, 2013 6:30 pm

Hi Marek,
Yes. As expected, we are smarter together :D
https://alternatic.ch

kyoush
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5
Joined: Thu Mar 14, 2013 12:44 pm

Re: Dependent Scrolling List Field

Post by kyoush » Tue Apr 23, 2013 8:31 am

Thanks for the stack Jean-Marc. I might have fumbled a few things had i done that from scratch. :)

And thanks for the 'upgraded' handler Marek. :D
I don't have much use of the toggleHilites property in my use case, but multipleHilite and NonContiguousHilite could be useful.

I did some modifications of my own. What i wanted for the MyListOut field was not all the entries in a single line but in different lines so that i can select one of them. At first i replaced the last put command in defMylistInMU to replace the commas with cr before putting it in MyListOut.
But then realized it would be much more efficient to do this when creating the array itself, so ended up modifying debDoArray instead.

Code: Select all

-- Buiding array
on debDoArray 
   put fld "mylist" into t 
   put empty into tTheCat
   repeat for each line tLine in t
      put item 1 of tLine into tCat
      put item 2 to -1 of tLine into tCont
      replace "," with cr in tCont
      put tCont into gMyArray[tCat]
      put tCat & cr after tTheCat
   end repeat
   delete char -1 of tTheCat
   put tTheCat into fld "MyListIn" 
end debDoArray
I also replaced the on mouseUp event handler with on selectionChanged so that the MyListIn field can be controlled using the arrow keys.

I've attached the modified stack. Do let me know if you think there's a more efficient way of achieving this.

I also hope i've earned my keep to ask another question...
With the MyListIn field being controllable with arrow keys, it feels natural to use the Tab key to move to the next field, i.e. MyListOut. But upon doing so, using the arrow keys does not change the selection, even though the dotted box is visible which shows the focus has moved. Continuing to Tab takes the focus to Field and MyList which can be edited, but the two lists are not controllable. Any suggestions?
Attachments
stArrayBasedDependentList_v2.zip
A List dependent on another List example using Arrays
(1.25 KiB) Downloaded 228 times
...Livecode newbie, finding my way... :)

kyoush
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5
Joined: Thu Mar 14, 2013 12:44 pm

Re: Dependent Scrolling List Field

Post by kyoush » Thu Apr 25, 2013 1:40 am

A little digging around, and a little tinkering.. solution found! A little arrowKey handler in the card script:

Code: Select all

-- List navigation with arrow keys
on arrowKey pTheKey
   if listBehavior of focusedObject is false then
      pass arrowKey
      exit arrowKey
   end if
   put the hilitedLine of focusedObject into tMyLine
   if pTheKey is up then
      put max(1,tMyLine-1) into tMyLine
   else if pTheKey is down then
      put min(number of lines of focusedObject,tMyLine+1) into tMyLine
   else
      pass arrowKey
   end if
   set the hilitedLine of focusedObject to tMyLine
end arrowKey
I must thank Mark and his post in another topic as i based the above off of his suggestion.
Updated stack attached.

@admins: if doable, could you change the topic title to prefix it with [Solved]. That should make it easier to spot.
Attachments
stArrayBasedDependentList_v2.livecode.zip
Dependent list field example, with arrow key list navigation
(1.83 KiB) Downloaded 250 times
...Livecode newbie, finding my way... :)

Post Reply