0

我是 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>

一切正常,除了当前项目不显示任何内容。

4

1 回答 1

0

尝试

<g:each in="${currentProject}" var="project">                                            
     ${fieldValue(bean: project, field: "name")}
</g:each>
于 2012-05-05T12:30:22.990 回答