使用grails中的列表

时间:2012-09-13 12:08:20

标签: list grails groovy

我正在尝试获取用户等于当前登录用户的域的每个实例。 我的代码是:

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)值:[[action:list,controller:child,max :10]]可能的解决方案:last(),first(),asList(),toList(),是(java.lang.Object),wait()

我确信必须有一种更简单,更好的方法。 任何想法?

4 个答案:

答案 0 :(得分:1)

您可以使用条件查询执行以下操作:

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

如果您的params有分页参数,那么“总数”将以childList.totalCount的形式提供,您无需单独计算。

答案 1 :(得分:0)

请添加域类。

但无论如何,我想,你的“其他”分支应该像:

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

}

答案 2 :(得分:0)

def childList = Child.findByUser(lookupPerson.username)

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

答案 3 :(得分:0)

如果您有Child和User类......为什么不这样做?...

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()