user3490014
user3490014

Reputation: 47

grails null value in dynamic select list

I have followed the tutorial on http://grails.org/AJAX-Driven+SELECTs+in+GSP and successfully created a drop down list that is populated based on the selection of a previous list. The problem I am running into is that I need to allow the auto populated list to have a null value as this field is not required. I am sure this is simple enough but I just can't seem to find any way to do this.

My code is the same as posted on the website above so I will avoid duplicating it here. Thanks!

Upvotes: 0

Views: 169

Answers (3)

john Smith
john Smith

Reputation: 17916

or use the "noSelection" attribute of

<g:select ... noSelection="['':'-none-']"/>

then the "rebuild-script" will rebuild this empty no-selection automatically

Upvotes: 0

D&#243;nal
D&#243;nal

Reputation: 187379

Rather than writing all the code yourself, you could either use this jQuery plugin or this Grails plugin to do some of the work for you. I haven't used the Grails plugin myself, but I have used the jQuery plugin and found it very helpful.

Upvotes: 0

Joshua Moore
Joshua Moore

Reputation: 24776

Right before the // Rebuild the select add in a blank option. For example:

var opt = document.createElement('option');
opt.text = ""
opt.value = ""
try {
  rselect.add(opt, null) // standards compliant; doesn't work in IE
} catch(ex) {
  rselect.add(opt) // IE only
}
// Rebuild the select

Upvotes: 1

Related Questions