I ran into an issue while populating domain object values on a gsp page by using a g:select tag.
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
In my controller, I have an object which gathers values from my domain. I tried writing this object in the controller both as:
def d = Demo.getAll()
and def d = Demo.list()
Both of these returned an exception while trying to use the g:select tag. The error was:
No signature of method: Demo.getAt() is applicable for argument types: (java.util.ArrayList) values: [[One, Two, Three, Four, Five]]
I was able to get my code working by simply removing the g:select tag and writing the following in my gsp:
<select name="demo" id="dm">
<option value="">Select!</option>
<g:each in="${dm}" status="i" var="dm">
<option value="${dm.id}">${dm.name}</option>
</g:each>
</select>
However, I would like to know how to make this work with the g:select tag, for my future reference.
Thanks in advance!
I see two issues in your select:
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${dm}"
optionValue="${dm.type}"/>
value shouldn't be looking at the collection for the id. I believe this is why you are getting your error. value should be whatever instance's id you've passed into your view.
optionValue doesn't required an expression.
Controller action
def someAction() {
def dm = Demo.get(params.id)
def demos = Demo.list()
[dm: dm, demos: demos]
}
view
<g:select name="demo" id="demo"
value="${dm.id}"
noSelection="${['null':'Select..']}"
from="${demos}"
optionValue="type"/>
Related
I am having a problem saving a domain instance in grails; The domainInstance that's passed to the default update method in my controller is NULL. The GSP page I am submitting from is not the default edit page. I have certain values from DB that need to be saved. I have the form tag defined on the page that contains the values I need to submit, as follows.
<g:form id="sampleForm" url="[resource:domainInstance, controller:'cntrlrName', action:'update']" method="PUT" >
I also have a version field which looks like this.
<g:hiddenField name="version" value="${domainInstance?.version}" />
My g:submit is as follows
<g:actionSubmit action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
Why is my domain instance null? What am I missing?
This is the common mistake one could make. The attribute id in <g:form> tag is not the id attribute of HTML tags but it is the id to use in the link for default mapping of Grails i.e.
"/$controller/$action/$id" {}
So change your tag as:
<g:form name="sampleForm" id="${domainInstance.id}" controller="cntrlrName" action="update" method="PUT">
You can pass the domainInstance but I feel it is better practice to pass the id instead of the object. Try passing the id of the domain instance and then reading the object in the controller.
<g:form name="sampleForm" action="action" controller="controller" id="${domainInstance.id}" ></g:form>
// in controller
def resource = Domain.read(params.id)
another approach could be to pass the domainInstance as a hiddenField
<g:hiddenField name="resource" value="$domainInstance" />
I'm using Grails 2.2.3 and Fields plugin 1.3.
I want to customize fields to manage one-to-many relationships using select tag.
In views/_fields/oneToMany/_input.gsp I have:
<g:select name="${property}.id" from="${type.list()}" optionKey="id" value="${value}" class="form-control one-to-many" noSelection="['null': "${label}"]" />
But type is a set, so I can't use list function.
How can I retrieve target domain class?
As long as you use a Map to declare the relationship, for example:
static hasMany = [ books: Book ]
You can get the list of the referenced domain, which is the key from the hasMany property of the bean, so the from attribute should change to
from="${bean.hasMany[property].list()}"
Alternatively you can pass the list to the _input.gsp template prefixing the variable name with input-, e.g.
<f:field property="books" input-domainList="${bookInstaceList}" />
In the _input.gsp template you can use the variable as follows:
from="${domainList}"
or mixing both methods:
from"${domainList ?: bean.hasMany[property].list()}"
I found this solution for the problem, this code work fine with one to many and one to one
<div class="input-group">
<g:select name="${persistentProperty.toString().contains('one-to-many')?property:property+'.id'}" value="${value?.id}" required="${required}"
noSelection="${['null':'Escriba...']}" class="select2 col-md-6"
multiple="${persistentProperty.toString().contains('one-to-many')?true:false}" from="${grailsApplication.getDomainClass(persistentProperty.properties['associatedEntity'].toString()).clazz.list()}"
optionKey="id"></g:select>
</div>
This solution can be helpful. I found in the documentacion of Fields Plugin, the persistentProperty is a class type DomainProperty but their methods doesn't works and I found the correct class is org.grails.datastore.mapping.model.MappingFactory
you can use .toArray() method on your Set instead of .list().
ale
You can use the code below to identify the Element of your collection
def className = bean.hasMany[property].properties.typeName
See my "views/_fields/oneToMany/_input.gsp" implementation:
<%# page defaultCodec="html" %>
<%
def className = bean.hasMany[property].properties.typeName
def simpleName = Class.forName(className).simpleName
def beanSimpleName = bean.class.simpleName
def createUri = "/${simpleName.toLowerCase()}/create?${beanSimpleName.toLowerCase()}.id=${bean.id}"
%>
<a id="add${simpleName}" name="add${simpleName}" href="${createLink(uri: "${createUri}")}">
Add ${simpleName}
</a>
<ul>
<g:each var="item" in="${value}">
<li>
${item}
</li>
</g:each>
</ul>
I'm a newbie trying to find uses for Neo4J on Grails.
Basically, I've made 20 grocery item nodes through the Neo4J browser and I want to create a simple Grails site that will let users search a grocery item and visually show the items related to it.
My index.gsp has:
<input id="item" />
My viz.js has:
$('#item').keyup(function() {
var item = $('#item').val();
My Item Domain class has
class Item {
static mapWith = "neo4j"
String name
My ItemController class has:
def index() {
def item = Item.list() [item:item] //No idea, just trying out whatever i find :(
and a query with something like:
def query = Item.cypherStatic ("""start n=node({Item}) match (n)-[r]->(x) where r='partner' return n, x)
Questions:
How can I properly send the JS 'item' variable into the ItemController?
How can I use the 'item' variable to properly query the node names which have a 'partner' relationship with the item?
in addition to Motilals answers, you definetly need a wrapping form with an action that points your controller
like
<g:form controller="itemController" action="index" >
<input type="text" id="item" name="item" value="" />
<input type="submit" value="submit" >
</g:form>
then on clicking submit the for will call your index action and there you could parse the value with
def item = params.item
but it looks more like you want some asynchronous stuff right after keyup-function, therefore you could do sth like this :
$('#item').keyup(function() {
var item = $('#item').val();
$.ajax({
url: "${createLink(controller:'itemController', action:'index')}",
data:"&item="+item
})
.done(function( data ) {
console.log(data)
});
});
in this case, you need to pay attention what your index-action is returning, so you can do in the .done() whatever you want with the response.
also note, that when you name an action "index" it will be available at
.../myproject/item/index
or, and thats important
.../myproject/item/
so if your index method requires the data from the input, it will miss them if a user has gone straight to that url
so your index action would rather render the page with the input
and you define another action for executing your query based on input and returning data
set the item to hidden field and then you can access it directly in your controller using params
here you go:
//in index.gsp add below hidden field and set the hidden filed in your js code
<g:hiddenField name="item" value="" />
$('#item').keyup(function() {
var item = $('#item').val();
//in your controller
def index() {
def item = params.item
print item // you see the value for item
//do your stuff
}
once you have item value you could directly use HQL query or use the domain instance
hope this helps you
Regards
Motilal
Say I have a Domain object Teacher with two fields String name, TeacherType teacherType, where TeacherType is an enum containing AssitantProfessor, AssociateProfessor, Professor.
After I generate the views using grails run-target generate-all Teacher, it produces an _form.gsp that is used for both create and edit of Teacher. In the edit view I want only the name to be editable but the TeacherType to be unmodifiable once created (this is just an example, it is a requirement that certain fields can't be updated after creation). In the create view, both TeacherType and name should be editable.
Since both create.gsp and edit.gsp render the _form template, what is the preferred approach here?
Create two separate templates i.e. _formCreate.gsp , _formEdit.gsp; Or
Pass in a model map within create.gsp and edit.gsp and use them in _form.gsp to conditionally render the view?
e.g.
In create.gsp:
<fieldset class="form">
<g:render template="form" model="[teacherInstance: teacherInstance, 'mode':'create']"/>
</fieldset>
In edit.gsp
<fieldset class="form">
<g:render template="form" model="[teacherInstance: teacherInstance, 'mode':'edit']"/>
</fieldset>
In _form.gsp
<g:if test="${mode == 'edit'}">
<g:select name="teacherType" from="${TeacherType?.values()}" keys="${TeacherType.values()*.name()}" required="" value="${teacherInstance?.teacherType?.name()}" disabled="disabled"/>
</g:if>
<g:else>
<g:select name="teacherType" from="${TeacherType?.values()}" keys="${TeacherType.values()*.name()}" required="" value="${teacherInstance?.teacherType?.name()}" disabled="false"/>
</g:else>
Approach 2 works but I suppose if the number of conditional statements increase it may just be better to follow approach 1 and split the forms.
Is there another approach that I'm not aware of?
The disabled attribute of <g:select> (and many other <g:...> form field tags) can be a boolean-valued expression:
<g:select name="teacherType" from="${TeacherType?.values()}"
keys="${TeacherType.values()*.name()}" required=""
value="${teacherInstance?.teacherType?.name()}"
disabled="${mode == 'edit'}"/>
This will render as disabled="disabled" if the expression evaluates to true, and as the absence of a disabled attribute (i.e. the field will not be disabled) if the expression is false. You could even use a boolean entry in the model, e.g. render the template with
model="[teacherInstance: teacherInstance, editing:true]"
(or editing:false respectively) and then say disabled="${editing}" on the <g:select>.
I need to create a custom gsp whose domain model is designed as follows
class Question {
SortedSet<Choice> choiceCollection;
static hasMany = [choiceCollection:Choice]
static mappping = {
choiceCollection(joinTable:false)
}
}
Each Question object has five Choices. So, i create the following snippet of code
create.gsp
<g:each var="i" in="${(1..5)}">
<div class="fieldcontain required">
<label for="description">
Option ${i}.
<span class="required-indicator">*</span>
</label>
<g:textArea name="choiceCollection[${i}].description" cols="40" rows="5" maxlength="2000" value="${questionInstance?.choiceCollection[${i}]?.description}"/>
</div>
</g:each>
Although Grails GSP engine complains Unclosed GSP expression which is not true - g:textArea is closed as you can see above -, i believe the real problem is the expression
${questionInstance?.choiceCollection[${i}]?.description}
which involves nested GSP expressions.
Question: am i missing some feature ? If so, what should i do to overcome my obstacle ?
Grails version: 2.1.1
Shouldn't
${questionInstance?.choiceCollection[${i}]?.description}
be
${questionInstance?.choiceCollection[ i ]?.description}
The set bit:
Try something like:
<g:each var="choice" status="i" in="${questionInstance?.choiceCollection}">
So i still contains your index, but choice contains what you were trying to get with questionInstance?.choiceCollection[${i}]