Optgroup in select ?

88 views
Skip to first unread message

Francois

unread,
Jan 26, 2010, 6:40:46 AM1/26/10
to Lift
Hello,

I'm wondering if it is possible to use "optgroup" tag with SHtml.select ?

If not, what would be the best way to use them ?


--
Francois ARMAND
http://fanf42.blogspot.com

David Pollak

unread,
Jan 26, 2010, 12:25:51 PM1/26/10
to lif...@googlegroups.com
On Tue, Jan 26, 2010 at 3:40 AM, Francois <fan...@gmail.com> wrote:
Hello,

I'm wondering if it is possible to use "optgroup" tag with SHtml.select ?

It's not possible today.  Do you have an idea of what the API should look like?
 

If not, what would be the best way to use them ?


--
Francois ARMAND
http://fanf42.blogspot.com

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Francois

unread,
Jan 26, 2010, 2:37:30 PM1/26/10
to Lift
Le 26/01/2010 18:25, David Pollak a écrit :
>
> It's not possible today. Do you have an idea of what the API should
> look like?

I believe that a simple select with a Seq[(String, Seq[(T,String)])]
could be sufficient.

On the other hand, there is already a lot of selectbox related methods
in SHtml, and I think that they miss some possibilities (for example,
adding attributes, especially class, to option).

So, I was thinking to something more general, like what is at the
bottom, with a "full feature" option model, group model, and "select
options".

Or perhaps it would be even nicer to have less select methods, with some
implicits for common use case ( Seq[String,String)] as option, in
particular)


Thanks for you time,

(the formating in the mail is bad, so I also join the source in a file)

8<------------ Code proposition for select model ------------------
/**
* A full featured option tag
*/
case class OptionModel[T](value:T, label:String, disabled:Boolean,
attrs:(String,String)*)

/**
* Option Group
*/
case class OptionGroupModel[T](label:String,
options:Seq[OptionModel[T]], disabled:Boolean, attrs:(String,String)*) {
def values = options map( _.value )
}

/**
* Options : list of ungrouped options
* OptionGroups : the list of groups of options.
*/
case class
SelectOptions[T](options:Seq[OptionModel[T]],groups:Seq[OptionGroupModel[T]])
{
def values = (options map( _.value)) ++ (groups map( _.values))
}

object SHtml {

// ....
private def selected(in: Boolean) = if (in) new
UnprefixedAttribute("selected", "selected", Null) else Null
// ....

private def disabled(in: Boolean) = if (in) new
UnprefixedAttribute("disabled", "true", Null) else Null

def select_*(opts: SelectOptions[String], deflt: Box[String], func:
AFuncHolder, attrs: (String, String)*): Elem = {
//build an <option> from an option model
def buildOption(option:OptionModel[String]) = {
option.attrs.foldLeft(
<option value={option.value}>{option.label}</option> %
disabled(option.disabled) % selected(deflt.exists(_ == option.value))
)(_ % _)
}

val testFunc = LFuncHolder(in => in.filter(v =>
opts.values.contains(v)) match {case Nil => false case xs => func(xs)},
func.owner)

attrs.foldLeft(fmapFunc(testFunc)(fn => <select name={fn}>{
//start with groups
opts.groups.flatMap { group =>
group.attrs.foldLeft(<optgroup label={group.label}>{
//each option in a groups, with its attributes
group.options.flatMap { buildOption(_) }
}</optgroup> % disabled(group.disabled) )(_ % _)
} ++
//now, option that are not in a group
opts.options.flatMap { buildOption(_) }
}</select>))(_ % _)
}

}
8<--------- end code proposition for select model -----------------

SelectModel.scala

Naftoli Gugenheim

unread,
Jan 26, 2010, 2:40:16 PM1/26/10
to lif...@googlegroups.com
Which method doesn't let you pass attributes?

-------------------------------------
Francois<fan...@gmail.com> wrote:


Thanks for you time,

object SHtml {

--

Francois Armand

unread,
Jan 26, 2010, 3:41:13 PM1/26/10
to lif...@googlegroups.com
Le 26/01/2010 20:40, Naftoli Gugenheim a �crit :

> Which method doesn't let you pass attributes?
>

All select methods do have an attrs:(String,String)* parameter, but as
far as I understand, attributes are applied to the <select>, not to
<option> tag, and sometime I really would like to be able to pass attr
to them (disabled or class for example)

--
Francois Armand
http://fanf42.blogspot.com

Reply all
Reply to author
Forward
0 new messages