2012-09-13 41 views
0

我想獲取用戶等於當前登錄用戶的域的每個實例。 我在一分鐘代碼:在grails中使用列表

def list(Integer max) { 
    params.max = Math.min(max ?: 10, 100) 
    if (lookupPerson().username == "admin"){ 
     adminList(max) 
    } 
    else{ 

     def childList = [] 
     def i = 1 
    Child.list().each(){ 
     if(Child.get(i).user.username == lookupPerson().username){ 
      def child = Child.get(i) 
      childList.add(child) 
     } 
      i++ 
    } 
     [childInstanceList: childList.list(params), childInstanceTotal: childList.count()] 
    } 


} 

這給了我下面的錯誤 法無簽名:java.util.ArrayList.list()是適用於參數類型:(org.codehaus.groovy.grails .web.servlet.mvc.GrailsParameterMap)values:[[action:list,controller:child,max:10]]可能的解決方案:last(),first(),asList(),toList() .Object),wait()

我確定必須有一個更簡單更好的方法來做到這一點。 有什麼想法?

+1

問題是在最後一行有'childList.list(params)'調用 - 你想在這裏實現什麼? –

+0

在我的子域我有創建子的用戶。我想列出由當前登錄的用戶創建的所有兒童。 – Sagarmichael

+0

@Sagarmichael你應該在列表中。然後你似乎嘗試在這個列表上調用'list(params)' –

回答

1

你或許可以做你一個條件查詢之後在做什麼:

def childList = Child.createCriteria().list(params) { 
    user { 
    eq('username', lookupPerson().username) 
    } 
} 

如果您params有分頁參數,則「總」將作爲childList.totalCount,你不需要來計算的話分別。

+0

我的IDE不喜歡這個,但它可以工作 – Sagarmichael

0

請添加域類。

但無論如何,我想,你的 「其他」 部門要lokk像:

Child.list().each { 
    if(it.user.username == lookupPerson().person) { 
     childList.add(it) 
    } 

} 
0
def childList = Child.findByUser(lookupPerson.username) 

def childList = Child.withCriteria { 
    eq("user", lookupPerson) 
} 
0

如果你有孩子和用戶類......爲什麼不這樣做這個?...

class Child { 
    //properties 
    User user 
} 

class User { 
    //properties 

    def getChilds() { 
    Child.findAllByUser(this) 
    } 
} 

然後你只需要在你的控制器上調用這個服務,查看:

def user = User.findByUsername(params.username) 
def childs = user.childs //or user.getChilds()