Skip to content

Commit

Permalink
Adding ajaxEditableSelect to SHtml. Allows users to dynamically add n…
Browse files Browse the repository at this point in the history
…ew selections into an ajaxSelect.
  • Loading branch information
gflanagan authored and fmpwizard committed Dec 14, 2012
1 parent aaebdc7 commit 66b20ad
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contributors.md
Expand Up @@ -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
32 changes: 32 additions & 0 deletions web/webkit/src/main/scala/net/liftweb/http/SHtml.scala
Expand Up @@ -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: _*)
Expand Down

0 comments on commit 66b20ad

Please sign in to comment.