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
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
--
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.
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 -----------------
-------------------------------------
Francois<fan...@gmail.com> wrote:
Thanks for you time,
object SHtml {
--
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