我是Groovy和grails的新手。我想在员工列表中显示员工的当前项目。我只能通过ProjectMember
课程获得员工的当前项目。我的想法是,我获得每个员工的当前项目,然后将其放在一个列表中,然后我在.gsp
中进行迭代。
class EmployeeController {
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
def currentProject = [ProjectMember];
List<Employee> employeeList = Employee.list(params)
System.out.print("PREV SIZE" + currentProject.size())
for(Employee emp: employeeList) {
def current = ProjectMember.findAllByEmployeeAndEndDateIsNull(emp, [sort: "project.name", order: "asc"])
System.out.print(current.project.name);
// This is working, I can get the current projects of the employee
if(!current.empty) {
currentProject.add(current);
// Here is the code I didn't understand I really don't know
// if the project is added in the list.
// Everytime I try to display the contents of the list using foreach,
// I always get an error. MissingProperty
}
}
[currentProject: currentProject,
employeeInstanceList: Employee.list(params),
employeeInstanceTotal: Employee.count()]
}
}
class ProjectMember {
Employee employee
EmployeeRole role
Date startDate
Date endDate
String notes
static belongsTo = [project: Project]
}
class Project {
String name
String alternateName
boolean useAlternateNameInResume = false
String summary
String duration
String skills
String technologies
Date startDate
Date endDate
static hasMany = [members: ProjectMember]
}
最后是观点,list.gsp
:
<g:each in="${employeeInstanceList}" status="i" var="employeeInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'} clickable" onclick="window.location='<g:createLink action='show' id='${employeeInstance.id}' />'">
<td>${employeeInstance.idNo?.encodeAsHTML()}</td>
<td>${fieldValue(bean: employeeInstance, field: "fullNameWithMiddleName")}</td>
<td>${employeeInstance.position}</td>
<td>
<g:each in="${currentProject}" var="currentProject" status ="j">
${fieldValue(bean: currentProject, field: "project.name")}
</g:each>
<g:if test="${currentProject.empty}">
No projects yet
</g:if>
</td>
</tr>
</g:each>
除了当前没有显示任何内容的项目外,一切正常。
答案 0 :(得分:0)
尝试
<g:each in="${currentProject}" var="project">
${fieldValue(bean: project, field: "name")}
</g:each>