kingpin_123
kingpin_123

Reputation: 55

Grails read from one domain to another domain

I created 2 domain class which is

Duty.groovy:

    class Duty {
               String username
               String duty
    }

User.groovy:

    class User {
               String username
    }

I already assigned some users in Bootstrap.groovy, I want to create a duty list function to assigned duty to users. So in my duty list, it should include a drop-down list which contains list of users from User domain.

But when I import testApp.User at my gsp (in Duty domain) file and created a taglib to display the userlist, my compiler shows that No such property. I would like to ask for suggestions for best way to display the list of users from my Duty domain to User domain?

Here is other code: SelectUserTagLib.groovy

    import HR_System.User
    def userInstance = new User()
    def displayType = { attrs,body -> 
    def name = attrs.name 

    out << "<select id=\"${name}\" name=\"${name}\" require=\"\" >"
    out << "<g:each in='${userInstanceList}'' status='i' var='userInstance'>"
    out << "<option value=\"${name}\">${fieldValue(bean: userInstance, field: "username")}</option>"
    out << "</g:each>"
    out << "</select>"
}

_form.gsp:

    <name:displayType name="username" />

Upvotes: 1

Views: 352

Answers (2)

injecteer
injecteer

Reputation: 20699

If you adjust your model to be really a domain model so it looks like:

    class User {
               String username
    }

    class Duty {
               User user
               String duty
    }

then Grails would scaffold the GSP-view for your Duty class automagically with the User-dropdown included

Upvotes: 1

Pavel Savchyk
Pavel Savchyk

Reputation: 362

I think you don't really need custom taglib for such purpose. You can do it with simple g:select.

<g:select name="user"
          from="${User.list()}"/>

You can read about it in grails docs: https://docs.grails.org/latest/ref/Tags/select.html.

Please ask if you have any questions.

Upvotes: 1

Related Questions