User.groovy
class User {
String firstName
String lastName
String email
Integer age
static constraints = {
firstName blank:false, nullable:false, maxSize:50
lastName blank:false, nullable:false, maxSize:50
email email:true, blank:false, nullable:false
age min:18, blank:false, nullable:false
}
}
UserController.groovy
class UserController {
static scaffold = true
def index() {
redirect(action: 'search')
}
def search() {
}
def searchTable(){
def list = User.list()
[ list:list ]
render(template: 'searchTable', model:[ list : list ])
}
}
的search.gsp
<h1>Please enter First Name or Last Name for search</h1>
<div id="lb">
<label for="first_name">First Name Search:</label>
<g:textField name="first_name"/><br/>
<br/><br/>
<label for="last_name">Last Name Search:</label>
<g:textField name="last_name"/><br/>
</div>
<div id="insertSearchResultsHere">
</div>
_searchTable.gsp
<%@ page import="com.myapp.User" contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<title>Users</title>
<style>
table, td, th {
border: 1px solid black;
}
td {
padding: 15px;
}
table{
width: 100%;
}
th {
background-color: green;
color: white;
text-align: left;
height: 50px;
}
</style>
</head>
<body>
<div id="list-menu" class="content scaffold-list" role="main">
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<g:each in="${list}" var="user" status="i">
<tr>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.email}</td>
<td>${user.age}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
</body>
</html>
如何使用firstName或lastName在search.gsp中搜索并使用grails中的remoteFunction从serachTable tamplate(在id="insertSearchResultsHere"
部分中显示)中返回数据。
答案 0 :(得分:0)
我认为在这种情况下remoteFunction不适合你 - 至少不容易,因为你需要在输入字段中收集用户提供的信息。
我认为你需要的是formRemote
- 毕竟,你正在渲染一个搜索表单 - 你还没有添加表单标签:)。所以:
为此,您只需要更改search.gsp
:
<h1>Please enter First Name or Last Name for search</h1>
<div id="lb">
<g:formRemote name="userSearch" url="${[controller: 'user', action: 'searchTable']}" method="GET" update="insertSearchResultsHere">
<label for="firstName">First Name Search:</label>
<g:textField name="firstName"/><br/>
<br/><br/>
<label for="lastName">Last Name Search:</label>
<g:textField name="lastName"/><br/>
<input type="submit" value="Search" />
</g:formRemote>
</div>
<div id="insertSearchResultsHere">
</div>
此外,您需要实际搜索控制器中提供的名字和姓氏:
def searchTable(){
def list = User.withCriteria {
and {
if (params.firstName) {
eq('firstName', params.firstName)
}
if (params.lastName) {
eq('lastName', params.lastName)
}
}
}
render(template: 'searchTable', model:[ list : list ])
}