gpt4 book ai didi

grails - 结合2种模型并在 View 中显示

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

我有2个模型类(class)。律师和任命。

律师模型

int id;
String firstName;
String gender;
String school;

附件模型
int id;
String appDate;
String appTime;
Lawyer lawyer;

首先,我想显示一个律师名单(这是 View )。律师列表应显示律师详细信息(姓名,年龄,学校 和ALOX 约会时间和日期)。

我如何加入这两个模型并在一个 View 中显示两个记录

View

我认为 View 中存在问题。最初,我使用脚手架生成 View 。那是问题所在吗?
<%@ page import="Pro.Appointment"%>

<%@ page import="Pro.Lawyer"%>
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName"
value="${message(code: 'appointment.label', default: 'Appointment')}" />

<g:set var="entityName"
value="${message(code: ' lawyer.label', default: ' Lawyer ')}" />
<title><g:message code="default.show.label" args="[entityName]" /></title>
</head>
<body>
<a href="#show-lawyerr" class="skip" tabindex="-1"><g:message
code="default.link.skip.label" default="Skip to content&hellip;" /></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message
code="default.home.label" /></a></li>
<li><g:link class="list" action="list">
<g:message code="default.list.label" args="[entityName]" />
</g:link></li>
<li><g:link class="create" action="create">
<g:message code="default.new.label" args="[entityName]" />
</g:link></li>
</ul>
</div>
<div id="show-lawyer" class="content scaffold-show" role="main">
<h1>
<g:message code="default.show.label" args="[entityName]" />
</h1>
<g:if test="${flash.message}">
<div class="message" role="status">
${flash.message}
</div>
</g:if>
<ol class="property-list lawyer">

<g:if test="${lawyerInstance?.firstName}">
<li class="fieldcontain"><span id="firstName-label"
class="property-label"><g:message
code="lawyer.firstName.label" default="First Name" /></span> <span
class="property-value" aria-labelledby="firstName-label"><g:fieldValue
bean="${lawyerInstance}" field="firstName" /></span></li>
</g:if>

<g:if test="${lawyerInstance?.lastName}">
<li class="fieldcontain"><span id="lastName-label"
class="property-label"><g:message
code="lawyer.lastName.label" default="Last Name" /></span> <span
class="property-value" aria-labelledby="lastName-label"><g:fieldValue
bean="${lawyerInstance}" field="lastName" /></span></li>
</g:if>

<g:if test="${lawyerInstance?.gender}">
<li class="fieldcontain"><span id="gender-label"
class="property-label"><g:message code="lawyer.gender.label"
default="Gender" /></span> <span class="property-value"
aria-labelledby="gender-label
<span
class="property-value" aria-labelledby="speciality-label"><g:fieldValue
bean="${lawyerInstance}" field="speciality" /></span></li>
</g:if>



ddfdfdfd
<g:each var="appointments" in="${lawyer}">
sdsd
<p>
firstdsdsdsName:
${lawyer.firstName}
</p>
<p>
gender:
${lawyer.gender}
</p>
<p>appointments:</p>
<ul>
<g:each var="app" in="${lawyer.appointments}">
<li>
${app.appointmentTime}
</li>
</g:each>
</ul>
</g:each>












</body>
</html>

最佳答案

首先,我建议您更进一步,以这种方式更新Lawyer模型:

String firstName
String gender
String school

static hasMany = [appointments: Appointment]

预约模式:
Date appDate
Date appTime

static belongsTo = [lawyer: Lawyer]

使用hasMany和belongsTo属性可将默认的Grails级联行为,保存和更新,保存,更新和删除(因此,如果您删除律师,则每个律师的约会也会被删除)。

要显示 的每个Lawyer和每个Lawyer约会的,您可以按照以下方式进行操作:首先在 Controller 方法中返回类似 [lawyers: Lawyers.list()]的内容,然后在与此方法相关的 View 中添加以下代码:
<g:each var="lawyer" in="${lawyers}">
<p>firstName: ${lawyer.firstName}</p>
<p>gender: ${lawyer.gender}</p>
<p>appointments:</p>
<ul>
<g:each var="app" in="${lawyer.appointments}">
<li>${app.Date}</li>
</g:each>
</ul>
</g:each>

请记住,Grails默认情况下使用延迟获取,因此可能未填充约会字段,因此将需要额外的查询。 See Grails fetching DSL docs

关于grails - 结合2种模型并在 View 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317745/

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