Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created August 29, 2011 03:36
Show Gist options
  • Save gclaramunt/1177727 to your computer and use it in GitHub Desktop.
Save gclaramunt/1177727 to your computer and use it in GitHub Desktop.
Lift code to generate a K/V create/update/delete
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))
}
}
@gclaramunt
Copy link
Author

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment