Search code examples
grailsgrails-orm

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" />

Solution

  • 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.