I have two domains declared in my app.
class Posts {
String title
String content
static hasMany = [tags:Tag]
static constraints = {
}
}
class Tag {
String Name
static belongsTo = Post
static hasMany = [posts:Post]
static constraints = {
}
String toString()
{
"Tag:${Name}"
}
}
I have a controller which manages the search and display of results:
package com.trafigura.com.trafigura
class ViewerController {
def defaultAction='search'
def search={}
def show = {
def _foundPost = Post.findAllBytitle(params.title)
return [posts: _foundPost, term: params.title]
}
}
The search.gsp code is as follows:
<html>
<head><title>Simple GSP page</title></head>
<body>Place your content here.
<formset>
<legend>TagsPosts</legend>
<g:form action="show">
<label for="title">Title</label>
<g:textField name="title" />
<g:submitButton name="search" value="Search"/>
</g:form>
</formset>
</body>
</html>
and the following code for show.gsp.
<html>
<head><title>Simple GSP page</title></head>
<body><h1>Results</h1>
for items matching <em>${term}</em>.
Found <strong>${posts.size()}</strong> hits.
</p>
<ul>
<g:each var="tag" in="${posts.tags}">
<li>${tag.Name}</li>
</g:each>
</ul>
<g:link action='search'>Search Again</g:link></body>
</html>
My Question is I am unable to display the tags as follows:
Results
Found 1 hits.
* [planting, dig]
However, I want the output as:
* planting
* dig
What am I doing wrong here?
Much Appreciated.
Replace
<g:each var="tag" in="${posts.tags}">
by
<g:each var="tag" in="${posts.tags[0]}">
Related
Here is my scenario:
Class Domain1 {
static hasMany=[ tests : Domain2 ]
static constraints = { tests(nullable: true) }
}
And
Class Domain2 {
Double t1, String t2
static constraints={
t1(nullable:true
t2(nullable:false,blank:false)
}
}
I need to display t1 from domain2 in domain1 with the ability to edit.
I need to display t1 from domain2 in domain1 with the ability to edit.
See the project at https://github.com/jeffbrown/samdomain.
grails-app/domain/samdomain/Domain1.groovy:
package samdomain
class Domain1 {
static hasMany = [tests: Domain2]
}
grails-app/domain/samdomain/Domain2.groovy:
package samdomain
class Domain2 {
Double t1
String t2
static constraints = {
t1 nullable: true
}
}
grails-app/controllers/samdomain/DemoController.groovy:
package samdomain
class DemoController {
def index() {
def d1 = new Domain1()
d1.addToTests t1: 42, t2: 'Fourty Two'
d1.addToTests t1: 2112, t2: 'Twenty One Twelve'
[domainInstance: d1]
}
def update() {
render "Updated values: ${params.t1Values}"
}
}
grails-app/views/demo/index.gsp:
<%# page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta name="layout" content="main"/>
<title>Simple Demo</title>
</head>
<body>
<g:form action="update" method="POST">
<table>
<g:each var="d2" in="${domainInstance.tests}">
<tr>
<td>${d2.t2}</td>
<td><g:textField name="t1Values" value="${d2.t1}"/></td>
</tr>
</g:each>
</table>
<g:submitButton name="Update"/>
</g:form>
</body>
</html>
I have a simple setup.
import java.sql.Date as SqlDate
class Book {
String name
SqlDate publishedDate
static constraints = {
}
}
In application.yml
grails:
databinding:
dateFormats:
- 'dd/MM/yyyy'
Book Controller save method
def save(){
def book = new Book()
bindData(book, params)
book.save()
render "done"
}
Create page
<g:form action="save">
<g:textField name="name"></g:textField>
<g:textField name="publishedDate"></g:textField>
<g:submitButton name="Submit"></g:submitButton>
</g:form>
When i use normal java date then the date binding works but when i use the sql.Date then bindData() doesnt seem to bind the date. Does bindData not work on sql.Date? Thanks for help!
Controller class
class EntidadController {
def index(){
def entidades = Entidad.list()
[entidades:entidades]
render(view:"index")
for (x in entidades)
{
print(x.nombreEntidad)
}
}
}
Domain class
class Entidad {
String nombreEntidad
int porcentaje
static hasOne = [kiosko: Kiosko]
static belongsTo = [adminCreador: Administrador,entidadSuperior: Entidad]
static hasMany = [adminEntidad: Administrador, entidadesInferiores: Entidad]
static constraints = {
kiosko nullable:true
nombreEntidad nullable : false
adminCreador nullable : true
adminEntidad nullable : true
entidadSuperior nullable : true
entidadesInferiores nullable : true
}
}
View in gsp
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<p>test</p>
<div controller="entidadController">
<g:each in="${entidades}" var="x">
<tr>
<td>${x.id}</td>
</tr>
</g:each>
</div>
</body>
</html>
I think code is fine but i dont know why is not showing anything at the g:each tag, ive tried with a static array in gsp page and g each worked and i print at console and the array "entidades " had objects on it, its like my view cant read data from controller
Perhaps a render statement like this:
render(view:'index', model:[entidades: entidades])
If you don't need pagination and sorting in your view, you can do like this on your gsp
...
<body>
<p>test</p>
<div controller="entidadController">
<g:each in="${Entidad.list()}" var="x">
<tr>
<td>${x.id}</td>
</tr>
</g:each>
</div>
</body>
...
The problem is here:
def index(){
...
[entidades:entidades]
render(view:"index")
...
}
You are not sending entidades to the view. This can be corrected this way (render docs):
def index(){
...
render(view:"index", model:[entidades:entidades])
...
}
Also, if your view and your action share the same name, you don't need to use render method explicitly, but the map as the last line of the action (this is a grails convention). Like this:
def index(){
...
[entidades:entidades]
}
I am working on a Grails form item that just populates a list of FaqCategories from an FaqCategory domain class. All I want the select dropdown to do is display the list. So far, this sort of works, but I'm not sure how to generate the list or what to put in the Value section if anything.
Form item:
<div class="col-sm-9">
<g:select name="Category" from="${FaqCategory.list()}" required="" class="form-control" aria-labelledby="faqCategory-label" value=""/>
</div>
Domain class #1
class Faq {
String question
String answer
FaqCategory faqCategory
static constraints ={
question nullable:false, maxSize:1000
answer nullable:false, maxSize:1000
faqCategory nullable:false
}
}
Domain class #2
class FaqCategory {
String categoryType
String toString(){
"$categoryType"
}
static constraints = {
categoryType nullable:true, maxSize:100, unique:true
}
}
Controller snippet
#Transactional(readOnly = true)
def create() {
respond new Faq(params)
}
Controller code :
yourAction(int id){
//do some
def faqInstance = Faq.get(id)
render[view:"form",model:[faqInstance:faqInstance]]
}
View code :
<g:select name="faqCategory"
from="${FaqCategory.list()}"
value="${faqInstance?.faqCategory?.categoryType}"
optionKey="id" />
Hope it helps..
You haven't provided enough information to show the context in which this select is being used so it is hard to say exactly what you need, but something like this should work:
<g:select name="faqCategory"
from="${FaqCategory.list()}"
value="${you only need this if you want to preselect a value}"
optionKey="id" />
See http://grails.org/doc/latest/ref/Tags/select.html for more details.
I hope that helps.
I am working on grails and facing the problem that how can I get a controller's (in my case, contactGroup) data which is hasMany relations with the other (contact) controller in my layout view. Below is my domain class codes...
class Contact {
String number
String name
...
static hasMany = [groups: ContactGroup, messages: Message]
static belongsTo = [Message, ContactGroup]
}
class ContactGroup {
String groupName
String description
...
static hasMany = [contacts: Contact, messages: Message]
}
Below is my layout view code which is meant to fetch all the groups with the number of contacts enclosed in parenthesis... Just like Gmail shows the contact and group list.
<h4>Contacts</h4>
<g:link controller="contact" action="list" class="active"><h5>All (${smsserver.Contact?.list().size()})</h5></g:link>
<g:layoutBody/>
<g:each in="${smsserver.ContactGroup}" var="c">
<g:link controller="contact" action="list">${c.findAll()}<br></g:link>
</g:each>
Please help me out.
You have got m:n relations. It means 1 contact has many groups and each group has many contacts. am I right? And you would like to show all groups and all contacts from group for given contact.
ok.
Get all groups for given contact:
${contact.groups} <%-- list of groups --%>
or more suitable
<ul>
<g:each in="${contact.groups}" var="group">
<li>
${group.groupName} (${group.contacts.size()})
</li>
</g:each>
</ul>
show all groups and contacts for given contact
<ul>
<g:each in="${contact.groups}" var="group">
<li>
<b>${group.groupName}</b> (${group.contacts.size()})
<ul>
<g:each in="${group.contacts}" var="c">
<li>
${c.name}
</li>
</g:each>
</ul>
</li>
</g:each>
</ul>
Your controller:
class ContactController {
def show(long id) {
def contact = Contact.get(id)
['contact': contact]
// or
// render view:'YourPage', model:['contact': contact]
}
}
for more details http://grails.org/doc/latest/guide/theWebLayer.html#controllers