Created
October 1, 2010 17:17
-
-
Save riffraff/606518 to your computer and use it in GitHub Desktop.
This file contains 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
trait Htmlizable { | |
def safeStr(obj:Any):String = if (obj == null) "" else obj.toString | |
def persistentFields: Array[Field] = | |
getClass.getDeclaredFields.filter( f=> | |
f.getAnnotation(classOf[Persistent]) != null && | |
f.getAnnotation(classOf[PrimaryKey]) == null | |
) | |
def toForm(action:String): scala.xml.Elem = | |
<form action={action}> | |
<fieldset> | |
<legend>Insert your data</legend> | |
{for (field <- persistentFields) | |
yield | |
<p> | |
<label for={field.getName}>{field.getName}</label> | |
<br/> | |
<input | |
type="text" | |
class="text" | |
id={field.getName} | |
name={field.getName} | |
value={safeStr(getClass.getDeclaredMethod(field.getName).invoke(this))}/> | |
</p> | |
} | |
<p> | |
<input type="submit" value="Submit"/> | |
<input type="reset" value="Reset"/> | |
</p> | |
</fieldset> | |
</form> | |
def toHtml(cssClass:String = null): scala.xml.Elem = { | |
val actualCssClass= if (cssClass == null) getClass.getName else cssClass | |
<div class={actualCssClass}> | |
{for (field <- persistentFields) | |
yield | |
<p> | |
<span>{field.getName}</span> | |
{safeStr(getClass.getDeclaredMethod(field.getName).invoke(this))} | |
</p> | |
} | |
</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment