« Return to Thread: g:select with multiple preselected values

Re: g:select with multiple preselected values

by hdockter :: Rate this Message:

Reply to Author | View in Thread

Hi Alex,

Alex Shneyderman wrote:
> I use the binding in the following way:
>
> If you look at the example in my original mail, I use 'repositoryKeys' as
> the value for the name property of the g:select element. After the form is
> submitted, the multiple selected values are automatically bound to the
> corresponding object. Grails is doing a good job here. In this example I'm
> actually interested in binding the repositories to my object, not the
> repositoryKeys. They are just a helper. Here is the class associated with
> the view.
>
> class Query {
>
>  List repositories
>
>  void setRepositoryKeys(List keys) {
>          repositories = keys.collect{Repository.REPOSITORIES[it]}
>  }
>
>  List getRepositoryKeys() {
>          repositories.collect{it.key}
>  }
> }
>
> I create getters and setters for repositoryKeys, although there is no field
> repository keys. Grails calls now automatically the setter, and passes the
> list of selected values to the setter. The setter maps the keys to my
> corresponding groovy objects. The other way round would work the same, if
> there were a way I could pass multiple preselected values to the g:select
> element. Then I could say:
>
> <g:select ... value="${query?.repositoryKeys}"/>
>
> I can't see how Grails can make this more convenient, except of course
> offering multiple preselection. What do you think?

It is a hack :-) And a reasonable one!

I think binding of multi-selectable references should behave just like the
binding of singly selectable reference.

I also think it is doable, just not implemented yet. For example:

class Query {
  List repositories
  static hasMany = [repositories:Repository]
}

binder has all the information it needs to bind from a list of rep ids
to the list of repository objects to fetch from DB. That is how the
simple reference bindings (select with one item selectable) are done.
Two things. I have forgotten to mention, that Query and Repository are command objects, introduced with 0.5. So there is no defined key like id for those classes.

Thanks for pointing to the automagic reference binding of domain objects. I haven't known this mechanism yet. I'm wondering if somehow grails can support reference binding for command objects, beyond the mechanisms I use in my hack.

The current reference binding is part of the domain class processing. The simple change of the g:select element dealing with multiple preselections, would have its value independent of improving reference binding.

I have added this feature to the g:select tag on my machine. There is now a property values, which accepts a list. Either value or values can bet set (or none). I gonna open a Jira issue and attach the patch.

- Hans

 « Return to Thread: g:select with multiple preselected values