我有2个模型类。律师和任命。
律师模型
int id;
String firstName;
String gender;
String school;
Appontment Model
int id;
String appDate;
String appTime;
Lawyer lawyer;
首先,我要展示律师名单(这是视图)。律师名单应显示律师的详细信息(姓名,年龄,学校和ALSO 预约时间和日期)。
如何加入这两个模型并在一个视图中显示两个记录
查看
我认为View中存在问题。最初我使用Scafolding生成视图。这会导致问题吗?
<%@ 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…" /></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>
答案 0 :(得分:1)
首先,我建议您更进一步,以这种方式更新律师模型:
String firstName
String gender
String school
static hasMany = [appointments: Appointment]
约会模式:
Date appDate
Date appTime
static belongsTo = [lawyer: Lawyer]
使用hasMany和belongsTo属性更改默认的Grails级联行为,保存和更新,保存,更新和删除(因此,如果删除律师,每个律师的约会也会被删除)。
要显示每位律师和每位律师的预约,您可以这样做:首先在您的控制器方法中返回[lawyers: Lawyers.list()]
之类的内容,然后在与此方法相关的视图中添加此代码:
<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
答案 1 :(得分:0)
Id
是自动生成的字段,其类型为Long
,因此您可以从两个模型中删除它。您还可以更新型号:
Lowyer模型:
String firstName
String gender
String school
static hasMany = [appointments: Appointment]
约会模式:
Date appDate
Date appTime
static belongsTo = [lowyer: Lowyer]
然后你可以使用像lower.appointments
这样的东西。
<强>更新强>
<g:each var="appointments" in="${lawyer}">
<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>
现在我认为您的问题是<g:each var="app" in="${lawyer.appointments}">
尝试类似<g:each var="app" in="${appointments}">