Created
August 29, 2011 03:36
-
-
Save gclaramunt/1177727 to your computer and use it in GitHub Desktop.
Lift code to generate a K/V create/update/delete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.liftweb._ | |
import net.liftweb.common._ | |
import common.Full | |
import http._ | |
import util._ | |
import js._ | |
import JsCmds._ | |
import xml.NodeSeq | |
import collection.immutable.Map | |
trait MapTableField { | |
var entries= Map[String,String]() | |
var keyEntry="" | |
var valueEntry="" | |
def tableId:String | |
def keyLabel:String | |
def valueLabel:String | |
def htmlTable(entries:Map[String,String]):NodeSeq = { | |
<table id={tableId} > | |
<tr><th>{keyLabel}</th><th>{valueLabel}</th><th>Actions</th></tr> | |
<tr><td>{SHtml.ajaxText(keyEntry, keyEntry=_)}</td><td>{SHtml.ajaxText(valueEntry, valueEntry=_)}</td><td>{SHtml.ajaxButton("+", add _)}</td></tr> | |
{entries.flatMap (kv => | |
<tr> | |
<td>{kv._1}</td><td>{kv._2}</td> | |
<td>{SHtml.a(()=>del(kv._1),{<div>X</div>})}</td> | |
<td>{SHtml.a(()=>update(kv),{<div>!</div>})}</td> | |
</tr>)} | |
</table> | |
} | |
def add = { | |
if (prevKey!=""){ entries=entries-prevKey; prevKey="" } | |
entries = entries+(keyEntry->valueEntry) | |
keyEntry="" | |
valueEntry="" | |
SetHtml(tableId, htmlTable(entries)) | |
} | |
def del(k:String)= { | |
entries= entries-k | |
keyEntry="" | |
valueEntry="" | |
SetHtml(tableId, htmlTable(entries)) | |
} | |
var prevKey="" | |
def update(kv:(String,String))={ | |
prevKey=kv._1 | |
keyEntry=kv._1; | |
valueEntry=kv._2 | |
SetHtml(tableId, htmlTable(entries)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO:
use a template instead of having the html in the code itself
generalize the container and operations (probably any Iterator[(A,B)] plus functions to add, update and delete will do it )