Friendly shortcuts Grails Scaffolding (instead of ID keys) for foreign key associations

I am trying to use Grails Scaffolding to quickly add a CRUD application around some old database tables (see https : //stackoverflow.com/questions/908161/... for the saga so far). I have already gone through the worst of problems and have a functional CRUD application, but there is one problem that remains with general usability.

Many of my domain objects have foreign key associations with other domain objects. A Contact owned by Owner , etc.

However, on the CRUD pages for Contact I don’t want to see the actual id key for Owner ... because it means nothing to users. I want the screen to display a more user-friendly value of Owner.name .

The "List" and "Show" Views explicitly handle all the attributes in the automatically generated View code, and I have the ability to customize this code to control the presentation. However, the "create" and "edit" views do not list all attributes. Instead, those views make some kind of Grail tag of type Grails as follows:

 ... <fieldset class="form"> <g:render template="form"/> </fieldset> ... 

This call seems to automatically detect during the execution of the field, and makes its own decisions about how to display them. For domain objects that have associations, this makes the wrong decision to display the associated gibberish object identifier rather than a more user-friendly attribute.

Is there a “simple” (or at least a “better way”) way to change the way the fields appear on the “edit” or “create” screen? Of course, this is a common problem when using forests is used with domain objects that have associations.

+6
source share
1 answer

Oh, du ... you can simply implement the " toString() " method for the associated domain object by returning it to the field that you want to use for display:

 class Owner { String id // not human-friendly String name // human-friendly // ...etc... String toString() { return name } } 

Now that you are in CRUD View mode for Contact , which has a field for its Owner association, what is displayed on the screen is the Owner.name attribute, not Owner.id or some ugly object reference.

+15
source

Source: https://habr.com/ru/post/908159/


All Articles