Reputation: 3897
The controller has the next method (I'm recycling my implementation of the bootstrap-file-upload plugin):
def uploadImage() {
String baseName;
String imageExtension = uploadPhotoService.imagesExtension;
String thumbnailExtension = uploadPhotoService.thumbnailsExtension;
switch(request.method){
case "GET":
def results = []
String imagesDirectoryPath = uploadPhotoService.getImageDirectoryDestinationPath(params.idAlojamiento);
def dir = new File(imagesDirectoryPath)
if( dir.exists() ) {
dir.eachFile {
baseName = uploadPhotoService.removeFileExtension(it.getName());
results << [
name: baseName,
size: it.length(),
url: createLink(controller:'alojamiento', action:'picture', params:[imageName: baseName + "." + imageExtension, idAlojamiento: params.idAlojamiento]),
thumbnail_url: createLink(controller:'alojamiento', action:'thumbnail', params:[imageName: baseName + "." + thumbnailExtension, idAlojamiento: params.idAlojamiento]),
delete_url: createLink(controller:'alojamiento', action:'deleteImage', params:[baseName: baseName, idAlojamiento: params.idAlojamiento]),
delete_type: "DELETE"
]
}
}
render results as JSON
break;
case "POST":
(...)
In the view, there is the next line:
<g:include controller="alojamiento" action="uploadImage" params="[idAlojamiento:alojamientoInstance.id]"/>
So the Internet browser shows a text line with the content of the JSON results variable:
[{"name":"boceto escaleras patio","size":37567,"url":"/AlojamientoPrototipo/alojamiento/picture?imageName=boceto+escaleras+patio.jpg&idAlojamiento=1","thumbnail_url":"/AlojamientoPrototipo/alojamiento/thumbnail?imageName=boceto+escaleras+patio.png&idAlojamiento=1","delete_url":"/AlojamientoPrototipo/alojamiento/deleteImage?baseName=boceto+escaleras+patio&idAlojamiento=1","delete_type":"DELETE"},
(...)
I don't want to show that text line. I want to loop through all images. I think it could work:
<g:each in="${results}">
<img src="${it.thumbnail_url}"/>
</g:each>
How could I to pass the results JSON variable to the GSP view to loop through it?
Upvotes: 1
Views: 4749
Reputation: 12238
To get a variable like that:
<g:set var="results" value="${g.include(controller: "alojamiento", action: "uploadImage", params: [idAlojamiento:alojamientoInstance.id])}" />
<g:each in="${JSON.parse(results)}">
<img src="${it.thumbnail_url}"/>
</g:each>
However I should mention that you should just send this down the initial call. Put the logic in a service to make it reusable.
Edit: Forgot it was a string
Upvotes: 3