gpt4 book ai didi

search - 我如何获取表数据,使用remoteFunction按firstName和lastName搜索

转载 作者:行者123 更新时间:2023-12-02 16:00:29 29 4
gpt4 key购买 nike

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>

如何在grails中使用remoteFunction通过firstName或lastName在search.gsp中进行搜索并以serachTable模板返回数据(显示在 id="insertSearchResultsHere"部分中)。

最佳答案

我认为remoteFunction在这种情况下不适合您-至少不容易,因为您需要在输入字段中收集用户提供的信息。

我认为您需要的是formRemote-毕竟,您正在呈现搜索表单-您只是尚未添加form标记:)。所以:

  • 用g: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>

    此外,您实际上需要按 Controller 中提供的名字和姓氏进行搜索:
    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 ])
    }

    关于search - 我如何获取表数据,使用remoteFunction按firstName和lastName搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639181/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com