Reputation: 475
We are trying to cleanup a groovy sql.rows result to only contain the values that come back from the query.
The query service is pretty straight forward
def getRowsFromDB(String sqlStatement) {
def sql = Sql.newInstance(-redacted-)
def rows = sql.rows (sqlStatement)
return rows
}
The list comes back like:
[SCBCRSE_SUBJ_CODE:SOCI, SCBCRSE_CRSE_NUMB:291, SCBCRSE_TITLE:Special Topics]
[SCBCRSE_SUBJ_CODE:SOCI, SCBCRSE_CRSE_NUMB:306, SCBCRSE_TITLE:Sociology of Work]
[SCBCRSE_SUBJ_CODE:SOCI, SCBCRSE_CRSE_NUMB:308, SCBCRSE_TITLE:Soc of Education]
[SCBCRSE_SUBJ_CODE:SOCI, SCBCRSE_CRSE_NUMB:312, SCBCRSE_TITLE:Criminal Adjudication]
[SCBCRSE_SUBJ_CODE:SOCI, SCBCRSE_CRSE_NUMB:314, SCBCRSE_TITLE:Extraordinary Group Behavior]
But we'd like to have it like:
SOCI 291 Special Topics
SOCI 306 Sociology of Work
SOCI 308 Soc of Education
SOCI 312 Criminal Adjucation
SOCI 314 Extraordinary Group Behavior
Is this something that could easily be done? I tried to add .value
to the .gsp tag but it throws an error saying no method found.
And finally when I do use this in my views there is always a set of { }
wrapping around each item in the list, how do you get rid of these?
Upvotes: 3
Views: 9033
Reputation: 50275
And if you do not want the result as String
but a list to iterate over and have control over in the view then use:
rows.collect{it.values()}
should give you
[[SOCI, 291, Special Topics],
[SOCI, 306, Sociology of Work],
[SOCI, 308, Soc of Education],
[SOCI, 312, Criminal Adjudication],
[SOCI, 314, Extraordinary Group Behavior]]
Upvotes: 9
Reputation: 171184
You could transform your list of GroovyRowResults into a list of Strings before returning them to the Gsp:
return rows.collect {
"${it.SCBCRSE_SUBJ_CODE} ${it.SCBCRSE_CRSE_NUMB} ${it.SCBCRSE_TITLE}"
}
Upvotes: 7