Jerome Ramos
Jerome Ramos

Reputation: 1

Populate Grails GSP table from multiple lists

I have two lists one called schemaList and the other is recordCount generated from different SQL queries. How can I populate one table with the two lists in the GSP? I can get one list to populate using g:each but not both. EDIT: Excerpts from StatsService.groovy:

//some sql connection code
sconn.eachRow ("select username as SCHEMA_NAMES from dba_users) {row ->
schemaList << row.SCHEMA_NAMES
}

//use schemaList.each() to iterate through schemas and create oracle proxy connections for each schema to get a count of the message_xml table
pconn.eachRow("select count(*) as rCount from message_xml") { row ->
recordCount << row.rCount
}

return [recordCount:recordCount]
return [schemaList:schemaList]

I'm trying to output it to something like this GetStats.gsp:

<thead>
<tr>
<th>Schema</th>
<th>Count</>
</tr>
</thead>
<tbody>
<g:each in="${schemaList} var="sl">
<tr>
<td>${sl}</td>
<g:each in="${recordCount}" var="rc">
<td>${rc}</td>
</tr>
</g:each>
</g:each>
</tbody>

but my table shows up blank when I do this.

I've only been doing Grails for about 2 months now but I really like it. Thanks in advance for the help!

Upvotes: 0

Views: 711

Answers (2)

Burt Beckwith
Burt Beckwith

Reputation: 75671

Having two return statements makes zero sense. That wouldn't compile in a Java class and only compiles in a Groovy class because it's allowed for other reasons. But whether it's Java, Groovy, or any other JVM language you only get to return one value from a method.

That method can contain multiple values of course, so you could create a small data object class with the two lists as fields and return that. But in this case since you're creating data to be passed to a GSP from a controller, you can use the standard Grails conventions. If you return something from a controller action method it's expected to be a Map. You can render directly, redirect, forward, etc., but those are all "void" returns.

Your controller should return a single map as the model, and it can have multiple values, so merge the two maps into one:

return [recordCount:recordCount, schemaList:schemaList]

and then include that in the model map in the controller (or use it as the whole model map).

Upvotes: 2

Ricardo Umpierrez
Ricardo Umpierrez

Reputation: 768

If I understand correctly you want to show list schemaList and list recordCount. Maybe you can on the controller do the logic to join two list i reffer something

def auxlist= schemaList + recordCount

Upvotes: 0

Related Questions