Permalink
Browse files

Adding ajaxEditableSelect to SHtml. Allows users to dynamically add n…

…ew selections into an ajaxSelect.
  • Loading branch information...
1 parent aaebdc7 commit 66b20ad9f817bc751e62bca67dddd4ec6cf2fad4 @gflanagan gflanagan committed with fmpwizard Dec 4, 2012
Showing with 38 additions and 0 deletions.
  1. +6 −0 contributors.md
  2. +32 −0 web/webkit/src/main/scala/net/liftweb/http/SHtml.scala
View
@@ -65,3 +65,9 @@ Francis Rhys-Jones
### Email: ###
francis.rhys-jones at guardian dot co dot uk
+
+### Name: ###
+Gregory Flanagan
+
+### Email: ###
+gregmflanagan at gmail dot com
@@ -935,6 +935,38 @@ trait SHtml {
attrs: _*)
}
+ /**
+ * Create a select box based on the list with a default value and the function
+ * to be executed on form submission with an additional selection that
+ * transforms select into an ajaxText allowing the user to add a new select option
+ * Warning: id attribute in attrs will get clobbered
+ *
+ * @param options -- a list of value and text pairs (value, text to display)
+ * @param default -- the default value (or Empty if no default value)
+ * @param func -- the function to execute when a new selection is made
+ */
+ def ajaxEditableSelect(opts: Seq[(String, String)], deflt: Box[String],
+ f: String => JsCmd, attrs: ElemAttr*): Elem = {
+
+ val id = nextFuncName
+ val textOpt = nextFuncName
+ val idAttr = Seq(ElemAttr.pairToBasic("id", id))
+ val options = opts :+ (textOpt , "New Element")
+ var _options = options
+
+ def addId(elem: Elem) = (idAttr.foldLeft(elem)(_ % _))
+
+ lazy val func: (String) => JsCmd = (select: String) => {
+ def text(in: String): JsCmd = {
+ _options = (in, in) +: _options
+ Replace(id, addId({ajaxSelect(_options, Some(in), func, attrs: _*)}))
+ }
+ if (select == textOpt) Replace(id, addId({ajaxText("", text(_), attrs: _*)})) & Focus(id) else f(select)
+ }
+
+ addId({ajaxSelect(options, deflt, func, attrs: _*)})
+ }
+
def ajaxSelect(opts: Seq[(String, String)], deflt: Box[String],
func: String => JsCmd, attrs: ElemAttr*): Elem =
ajaxSelect_*(opts, deflt, Empty, SFuncHolder(func), attrs: _*)

0 comments on commit 66b20ad

Please sign in to comment.