我尝试创建表单以编辑一对多关系。我创建了两个类:
class StudentGroup extends LongKeyedMapper[StudentGroup] with IdPK with OneToMany[Long, StudentGroup] {
object groupName extends MappedString(this, 20)
object students extends MappedOneToMany(Student,Student.studentGroup)
}
class Student extends LongKeyedMapper[Student] with IdPK {
object firstName extends MappedString(this,35)
object lastName extends MappedString(this,35)
object studentGroup extends MappedLongForeignKey(this, StudentGroup)
}
然后我创建了一个代码段来显示一个包含以下代码的组:
val addStudentContent: NodeSeq = <div>
<input type="text" placeholder="First Name"></input>
<input type="text" placeholder="Last Name"></input>
</div>
def addStudent = AppendHtml("studentlist", addStudentContent)
def render =
"#addstudent" #> SHtml.ajaxButton("Add student", () => addStudent)
点击&#39;添加学生&#39;按钮出现两个新字段,我可以在那里输入名字和姓氏。问题是 - 如何将这些数据存储在数据库中?任何提示?
由于
答案 0 :(得分:1)
您可以通过多种方式开始解决此问题。我自己从不使用Mapper
,所以可能有比我建议的更好的方法。但是,像Ajax example in Simply Lift这样的东西可以让你开始。将它应用到您的代码中,我们会得到如下内容。
object StudentSnippets {
val addStudentContent: NodeSeq = <form data-lift="form.ajax">
<div data-lift="StudentSnippets.ajaxSubmit"> // Calls ajaxSubmit below
<input name="first" type="text" placeholder="First Name"></input>
<input name="last" type="text" placeholder="Last Name"></input>
<input type="submit" value="Submit"></input>
</div>
</form>
def addStudent = AppendHtml("studentlist", addStudentContent)
def render =
"#addstudent" #> SHtml.ajaxButton("Add student", () => addStudent)
private object first extends RequestVar("")
private object last extends RequestVar("")
def ajaxSumbit = {
"name=first" #> SHtml.text(first.is, first(_), "id" -> "the_name") &
"name=last" #> (SHtml.text(last.is, last(_)) ++
SHtml.hidden(ajaxProcess)) // Calls ajaxProcess below
}
def ajaxProcess = {
println("First is "+first.is)
println("Last is "+last.is)
Noop // This is the JS that will run after submit.
}
}
您可以查看另一种方法here on the wiki。
有关Lift的更多帮助,我强烈建议您将问题发布到Lift Google Group。你会发现,与其他论坛如SO。
相比,你更多地了解了Lift的帮助