In order to do reporting on my domain, I have a need to pass multiple parameters through the URL and also pass their corresponding values.
Simple Example - Single value for each
http://localhost/myapp/search?
filtertype=status&filterterm=pending
Complex Example - Multiple values -
http://localhost/myapp/search?
filtertype=status&filtertype=color&filterterm=pending&filterterm=red
I can get these values as a list using params.list("filtertype")
& params.list("filterterm")
but this way they won't have any correlation among them.
My question is how would I make my criteria query based on these values such that I don't have to hardcode the array index for filterterm
as I am doing in the solution below?
def filteredResults(params) {
def filtertypes = params.list("filtertype")
def filterterms = params.list("filterterm")
def list = MyDomain.createCriteria().list(max: params.max?: 10, offset: params.offset?: 0) {
if ("status" in filtertypes) {
eq("status", filterterms[0])
}
if ("color" in filtertypes) {
eq("color", filterterms[1])
}
}
}
Note that in the above solution I am hard coding filterterms[0] etc..
this will not scale well. I need a solution that will scale well to multiple values.
Have you tried using eachWithIndex
like this?:
def filteredResults(params) {
def filtertypes = params.list("filtertype")
def filterterms = params.list("filterterm")
def list = MyDomain.createCriteria().list(max: params.max?: 10, offset: params.offset?: 0) {
filtertypes.eachWithIndex { ft, idx ->
eq("${ft}", filterterms[idx])
}
}
}