2012-05-05 28 views
0

我是Groovy和Grails中的新手。我想在員工列表中顯示員工的當前項目。我只能通過ProjectMember課程獲得當前的員工項目。我的想法是,我得到每個員工的當前項目,然後將其放在一個列表中,然後我在.gsp中進行迭代。無法從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

是的,。沒有顯示。我的代碼有什麼問題嗎? – mukumuku

+0

currentProject被初始化爲ProjectMember.class作爲其單個成員的列表。這看起來很奇怪,可能會導致gsp在嘗試獲取類'project.name屬性時出現意外的行爲。也許「def currentProject = []」將是正確的初始化? – johanneslink

回答

0

嘗試

<g:each in="${currentProject}" var="project">            
    ${fieldValue(bean: project, field: "name")} 
</g:each> 
+0

仍然。什麼都沒發生。我認爲它不會進入循環。 – mukumuku

+0

嘗試更新的版本 –

+0

如果這樣工作,因爲定義的var(currentProject)從未在代碼中引用,我會非常驚訝。 – johanneslink