How to make ID column visible in the list view - grails

I have the following Groovy Domain class and generated scaffold controller for it.
When I create a Book row in the table, the ID column is invisible on the list view.
Is there anyway to make ID column visible on the view? I tried visible:true but it seems not making any difference.
class Book {
String bookAuthor
static constraints = {
bookAuthor blank: false, maxSize:30
}
static mapping = {
version false
id generator: 'sequence',
params: [sequence:'s_book_seq']
}
}

Edit the generated scaffolding views and add an id field manually. If you need to do this for a large number of domain classes, modify the scaffolding templates instead; you can install the templates using grails install-templates, they will be copied into src/templates

Related

Grails data won't bind if the id column is renamed

Grails will create an id and a version columns from a domain class automatically. I want to use my own column for the primary key. So, I follow the doc to change the mapping.
class book {
String isbn
static mapping = {
id generator: 'assigned', name: 'isbn'
}
}
So far so good. The isbn column is now the primary key.
I use generate-all to create the view and controller. However, the data binding won't work anymore.
Create and Save work no problem. It binds a book to the view. I can add a new book to the database no problem.
def create() {
respond new Book(params)
}
def save(Book book) {
if (book == null) {
notFound()
return
}
...
}
But the Update action does not bind. book is null after I click the Update button from the Edit view.
def update(Book book) {
if (book == null) {
notFound()
return
}
...
}
The codes generated by generate-all in the Save and Update actions are the same. I don't understand why it will bind the book to the Save action but not to Update action.
Would you show me the problem please?
Many Thanks!
I think I figure it out. When I bind an object to a view, Grails is hardcoded to look for the id property. It has to be spelled "id". If there is no "id" property in the domain class, Grails will not bind.
The way I figure this out is to look at the actual HTML generated by the server.
If there is an id property bind to the view, I see the HTML has the ../controller/action/id link.
If the id property is missing, the HTML link is just ../controller/index
I am new to Grails. So, I guess in order for the binding to work, I need to have an id property for Grails to put in the link.
I think this is a REST call. I don't know what REST is though.
So, I will try to add an dummy id property to my Book domain class to see if Grails will take the bait. I will set up the domain so Grails won't generate the id column in the database table. The id property is used locally only. No need to save it to the database.
class book {
String isbn
String id
static mapping = {
id generator: 'assigned', name: 'isbn'
}
}
I will copy the isbn value to the id property. I am not sure if this will work or not. I hope Grails will generate the link in the view with the isbn string in the id property instead of the default integer id value.
../controller/action/978-3-16-148410-0

how to make first column nullable in one to many relationship join table?

There seems to be a change in behavior from grails 2 to grails3. When i create a one to many relationship like
class Author {
static hasMany = [books: Book]
String name
}
class Book {
String title
}
It will create a join table with columns author_books_id and book_id. In grails 3 it also adds a not null constraint on the first column. In grails 2 not null constraint is not applied. So when i upgrade to grails 3 it is breaking because there are already few records that have first column values to null. It works fine in grails 2 but with grails 3 the first column should not be null. Furthermore the join table is read only so i cannot remove the rows will null first column values. Is there a way to make the first column nullable = true by making changes in domains and not directly in migration file.
The code was extracted from the grails documentation. Please scroll down to one to many section.
6.2.1.2 One-to-many
http://docs.grails.org/3.0.17/guide/GORM.html
Try changing your Book class to this
class Book {
String title
Author author // this creates the belongs to relationship
}
static constraints = {
author nullable:true
}
Also when you recompile the code make sure to delete previous table and start fresh. Grails will not alter any table to delete anythings. It only adds new stuffs.

Grails: display linked table value in list.gsp instead of id

Let me preface this ask by indicating that this only applies to /list/ view and NOT other views. This is NOT about drop-down menus
I have two tables:
[Authors]
- id
- name
[Books]
- id
- title
- author
In the Books form (create/edit) I can get the "author" field to display an author's name (from Author table instead of id) without an issue, however, when I view the /list/ view, it displays the author's id instead of the name (correctly, since this is what's stored in the DB).
I have not been able to find a method to convert "id" to "name" in the /list/ view elsewhere and I haven't used grails in almost a year (yay rust). Thanks in advance for guidance.
Try this:
class Author {
String name
static mapping = {
id name: 'name'
}
}
If you're using static scaffolding, don't forget to regenerate the views for the Author and Book domain classes:
grails> generate-views com.yourPackage.Author
grails> generate-views com.yourPackage.Book

grails: scaffolding create view for domain with hasMany relation

Let's assume the following (simplified) domain classes:
class Person {
static hasMany = [stringProperties: StringProperty]
static constraints = {
}
}
and
class StringProperty {
String name
String value
static constraints = {
name blank:false
value blank: true
}
}
When scaffolding generates the create view, there is no option in the gsp to create a StringProperty from Person.
Does a plugin exist or does somebody know a best practice, that can render a sort of create ui that allows to create members of a hasmany relation.
I'm just asking before i take the time to modify the scaffolding templates.
This is one of the areas where a plugin or enhanced scaffolding would be welcomed by the community. If I had the time I would take the information presented here and make a plugin for it. I have used this approach a few times and it works well.

How to insert data to four tables from one view in Grails?

I have four domain classes in my Grails based web application (first I've ever made):
class Receipt
{
String title
Date dateCreated
static hasMany = [articles: Article]
static constraints =
{
title blank: false, unique: true
}
}
class Article
{
String name
Quantity quantity
TypeOfArticle typeOfArticle
static hasOne = [quantity:Quantity, typeOfArticle:TypeOfArticle]
static constraints =
{
quantity unique: true
}
}
class Quantity
{
Integer quantity
Article article
}
class TypeOfArticle
{
String type
Article article
}
How can I add one Receipt together with all details on one view?
I want to make it look like on this picture:
http://i.stack.imgur.com/uNVzW.png
I hope you'll be able to help me! Thank you in advance!
You might want to look up Command Objects. It looks like you are trying to have someone enter data into one form that spans multiple classes. You can create a Command Object that has fields from all four classes. You use that object for the gsp fields, then capture in the controller method and validate the data; then create your actual Receipt, Article, etc.
You can get parameter in your controller action,In action you can simply write object.propertyName=params.propertyName and after getting all parameters you can simply store it by calling object.save(). you can do the same process for all four tables..

Resources