Adding Attributes to Options in Select

35 views
Skip to first unread message

Matt Farmer

unread,
Mar 26, 2013, 11:53:17 AM3/26/13
to lif...@googlegroups.com
Hey folks,

So, I found a few tickets that indicate there's been some discussion of this in the past, but I just recently had to deal with it again - so I figured it was worth bringing up: there's no easy way (that I can see) to set attributes on parameters of option tags inside a select statement. I recently had to do this for Anchor Tab (which is now live by the way! yay!) so that I could determine on the client side which plans required credit card information before you would be allowed to create your account. I did that using this code...

object AuthenticationSHtml extends SHtml {
 
private def selected(in: Boolean) = if (in) new UnprefixedAttribute("selected", "selected", Null) else Null

 
/**
   * Create a select box based on the list with a default value and the function to be executed on
   * form submission
   *
   * @param opts -- the options.  A list of value and text pairs
   * @param deflt -- the default value (or Empty if no default value)
   * @param func -- the function to execute on form submission
   */

 
def selectPlans(opts: Seq[(String, String, String)], deflt: Box[String],
               strFunc
: (String)=>Any, attrs: ElemAttr*): Elem = {
    val func
= S.SFuncHolder(strFunc)
    val vals
= opts.map(_._2)
    val testFunc
= S.LFuncHolder(in => in.filter(v => vals.contains(v)) match {case Nil => false case xs => func(xs)}, func.owner)

    attrs
.foldLeft(S.fmapFunc(testFunc)(fn => <select name={fn}>{opts.flatMap {case (hasTrial, value, text) => (<option data-has-trial={hasTrial} value={value}>{text}</option>) % selected(deflt.exists(_ == value))}}</select>))(_ % _)
 
}
}

... and then using the selectPlans implementation to create the dropdown. (I wanted to break that foldLeft into multiple lines, but the compiler would have none of it... sorry for the ugly.) I'm now in a situation where I need to do the same for work, and I'm just thinking that this is quite a lot of overhead to do something that should be relatively simple. That said, I don't particularly like my solution of adding a third item to the tuple either. Would anyone be opposed to implementing something in SHtml proper that we can use to set these attributes properly on option tags for the various kinds of selects?

I feel like the improvement should allow you to specify your options like so...

val options =
 
SelectOption(valueParam1, textParam1) ::
 
SelectOption(valueParam2, textParam2, "data-has-trial" -> true) ::
 
Nil


... but provide some kind of conversion for backward-compatibility with older code. (Possibly with a deprecation warning attached?)

Anyone have some love/hate/suggestions for me?

David Pollak

unread,
Mar 26, 2013, 12:09:52 PM3/26/13
to lif...@googlegroups.com
+1

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net

Andreas Joseph Krogh

unread,
Mar 26, 2013, 4:34:24 PM3/26/13
to lif...@googlegroups.com
I use this signature for "options": options: Seq[(Option[T], List[SHtml.ElemAttr])]
 
SelectField depends on quite a lot of other stuff but feel free to use it for inspiration if it helps. A have a version which supports opt-groups too but haven't published it to the example-project yet.
 
--
Andreas Joseph Krogh <and...@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 

Matt Farmer

unread,
Mar 26, 2013, 5:51:11 PM3/26/13
to lif...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages