如何将从数据库中检索到的数据和 return table 的数据编辑到表单的文本字段
How to edit retrieved data from database and return table's data to the form's text fields
我无法编辑从我的数据库 (MySQL) 中检索到的 table 行,当我按下编辑铅笔时,我的表单中什么也没有得到,但是对于删除它在我想要的任何行。
这是我获得 table 数据
的 history.scala.html
文件
@(formList: List[Users],form: Form[Users])
@main("history") {
@for(i <- mtnUsers.all()) {
@if(i.client.equalsIgnoreCase("potato")) {
<table>
<tbody>
<tr>
<td> <a href="/#"><i>@i.firstname @i.lastname </i></a></td>
<td> <a href="/#"><i>@i.phonenumber</i></a></td>
<td> <a href="/#"><i>@i.amount</i></a></td>
<td> <a href="/#"><i>@i.doneAt</i></a></td>
<td> <a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil"></span></i></a></td>
<td> <a href="@routes.Application.delete(i.id)"><i><span class="glyphicon glyphicon-trash"></span></i></a></td>
</tr>
</tbody>
</table>
}
}
}
以下是我的控制器编辑方法和检索方法之一
public static Result history(long id) {
Form<Users> taskData = form(Users.class);
return ok(history.render(Users.MTN(), taskData));
}
public static Result edit(Long id) {
Form<Users> content = form(Users.class).fill(
Users.find.byId(id));
return ok(views.html.edit.render(id,content));
}
public static Result update(Long id) {
Form<Users> updateForm = form(Users.class).bindFromRequest();
if (updateForm.hasErrors()) {
return badRequest(edit.render(id, updateForm));
}
updateForm.get().update(id);
return redirect(routes.Application.profile());
}
和edit.scala.html
,应该获取待修改数据的表单
<form action="@routes.Application.update(id)" method="post">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Phone Number</label>
<div class="col-sm-10">
<input name="phonenumber" class="form-control" type="text" placeholder="Phone Number">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">First Name</label>
<div class="col-sm-10">
<input name="firstname" id="firstname" minValue="2" class="form-control" type="text" placeholder="First Name">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Last Name</label>
<div class="col-sm-10">
<input name="lastname" id="lastname" class="form-control" type="text" placeholder="Last Name">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">User</label>
<div class="col-sm-10">
<select class="form-control" name="client" id="client" value="client">
<option value="select">select ..</option>
<option value="potato">potato</option>
<option value="kamagaju">kamagaju</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Amount</label>
<div class="col-sm-10">
<input name="amount" id="" value="" class="form-control" type="number" placeholder="Rwfr">
</div>
</div>
<div class="panel-footer">
<input type="submit" class="btn btn-danger" value="Send">
<input type="reset" class="btn btn-warning" value="Reset">
</div>
</form>
我最后所做的是在我的 edit.scala.html 模板中添加以下以粗体标记的脚本。在我添加的每个编辑文本值中:
value="@data.id_of_this_filed"
@(data:Users,customerForm: Form[Users])
@main("edit") {
<form ... >
.....
<div class="form-group row ">
<label class="col-sm-2 form-control-label">Amount</label>
<div class="col-sm-10">
<input
name="amount"
id=""
**value="@data.amount"**
class="form-control"
type="number"
placeholder="Rwfr">
</div>
</div>
.......
</form>
}
我无法编辑从我的数据库 (MySQL) 中检索到的 table 行,当我按下编辑铅笔时,我的表单中什么也没有得到,但是对于删除它在我想要的任何行。
history.scala.html
文件
@(formList: List[Users],form: Form[Users])
@main("history") {
@for(i <- mtnUsers.all()) {
@if(i.client.equalsIgnoreCase("potato")) {
<table>
<tbody>
<tr>
<td> <a href="/#"><i>@i.firstname @i.lastname </i></a></td>
<td> <a href="/#"><i>@i.phonenumber</i></a></td>
<td> <a href="/#"><i>@i.amount</i></a></td>
<td> <a href="/#"><i>@i.doneAt</i></a></td>
<td> <a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil"></span></i></a></td>
<td> <a href="@routes.Application.delete(i.id)"><i><span class="glyphicon glyphicon-trash"></span></i></a></td>
</tr>
</tbody>
</table>
}
}
}
以下是我的控制器编辑方法和检索方法之一
public static Result history(long id) {
Form<Users> taskData = form(Users.class);
return ok(history.render(Users.MTN(), taskData));
}
public static Result edit(Long id) {
Form<Users> content = form(Users.class).fill(
Users.find.byId(id));
return ok(views.html.edit.render(id,content));
}
public static Result update(Long id) {
Form<Users> updateForm = form(Users.class).bindFromRequest();
if (updateForm.hasErrors()) {
return badRequest(edit.render(id, updateForm));
}
updateForm.get().update(id);
return redirect(routes.Application.profile());
}
和edit.scala.html
,应该获取待修改数据的表单
<form action="@routes.Application.update(id)" method="post">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Phone Number</label>
<div class="col-sm-10">
<input name="phonenumber" class="form-control" type="text" placeholder="Phone Number">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">First Name</label>
<div class="col-sm-10">
<input name="firstname" id="firstname" minValue="2" class="form-control" type="text" placeholder="First Name">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Last Name</label>
<div class="col-sm-10">
<input name="lastname" id="lastname" class="form-control" type="text" placeholder="Last Name">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">User</label>
<div class="col-sm-10">
<select class="form-control" name="client" id="client" value="client">
<option value="select">select ..</option>
<option value="potato">potato</option>
<option value="kamagaju">kamagaju</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 form-control-label">Amount</label>
<div class="col-sm-10">
<input name="amount" id="" value="" class="form-control" type="number" placeholder="Rwfr">
</div>
</div>
<div class="panel-footer">
<input type="submit" class="btn btn-danger" value="Send">
<input type="reset" class="btn btn-warning" value="Reset">
</div>
</form>
我最后所做的是在我的 edit.scala.html 模板中添加以下以粗体标记的脚本。在我添加的每个编辑文本值中:
value="@data.id_of_this_filed"
@(data:Users,customerForm: Form[Users])
@main("edit") {
<form ... >
.....
<div class="form-group row ">
<label class="col-sm-2 form-control-label">Amount</label>
<div class="col-sm-10">
<input
name="amount"
id=""
**value="@data.amount"**
class="form-control"
type="number"
placeholder="Rwfr">
</div>
</div>
.......
</form>
}