Reputation: 413
Good evening. I'm having a problem displaying Grails domain object information in a Vaadin grid. This is what i have so far:
contenedorClientes = new BeanItemContainer<Cliente>(Cliente.class, Grails.get(ClientesService).obtenerClientes())
gdClientes = new Grid()
gdClientes.containerDataSource = contenedorClientes
Basically, what i am doing is this: first, i create a BeanItemContainer and then i set this configure this container to be one of type Cliente, and i also set the source of the data for this container which in this case is a method of a Grails service which returns a list of objects of type Cliente.
Then, I instantiate a Vaadin grid and set its containerDataSource to the container created before.
The main problem that i am having is that the grid is also displaying information from the domain class which Cliente extends from. This means that properties like Version, Dirty, Meta Class are being displayed as well. I do not want this, i just want the data from the Domain class that i created to be displayed.
Here is the domain class:
class Cliente {
String nombre
String apellido
String telefono
String email
static hasOne = [usuario:Usuario]
static constraints = {
nombre(nullable: false, blank: false)
apellido(nullable: false, blank: false)
telefono(nullable: false, blank: false, matches: '^\\d{3}-\\d{3}-\\d{4}$', unique: true)
email(nullable: false, blank: false, email: true, unique: true)
}
}
What do i need to do in order to display only the information in this class, and not the one in the super class from which it derives?
Also, does anyone know how to set the order of rendering of the columns in the grid?
Thank you in advance for your help.
Upvotes: 0
Views: 116
Reputation: 413
I solved this by using the method addColumns of the grid and specifying what columns I wanted to show. No need to remove columns.
Upvotes: 0
Reputation: 453
You want to remove the unnused columns from the container before adding your items
List<Cliente> itemsList = Cliente.listOrderById()
BeanItemContainer<Cliente> container = new BeanItemContainer<Cliente>(Cliente.class, Grails.get(ClientesService).obtenerClientes())
//remove useless columns that cause problems
def removed = ["id","errors","attached","dirty","dirtyPropertyNames","properties","metaClass","version","token"] //you get the idea
removed.each {container.removeContainerProperty(it)}
// then considering that you have removed everything you want, finally add your items ot the container
container.addAll(itemsList)
Upvotes: 0
Reputation: 37073
A general good first read is the Grid section in the Book of Vaadin.
The general approach goes like this: first you remove all columns with removeAllColumns
after you have set the container and then you keep adding the actual column configs. Since you control this step the order of the columns comes naturally (if you really just want to reorder all existing, use setColumnOrder
).
This is a standalone example for the approach:
// run with `spring run --watch vaadin.groovy`
@Grapes([
@Grab('org.vaadin.spring:spring-boot-vaadin:0.0.5.RELEASE'),
@Grab('com.vaadin:vaadin-server:7.5.10'),
@Grab('com.vaadin:vaadin-client-compiled:7.5.10'),
@Grab('com.vaadin:vaadin-themes:7.5.10'),
])
import org.vaadin.spring.annotation.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.*
import com.vaadin.annotations.*
import com.vaadin.data.util.*
import java.time.*
class Client {
String uuid
String name
LocalDate date
}
@VaadinUI
@Theme('valo')
class MyUI extends UI {
protected void init(VaadinRequest request) {
def clientList = (1..10).collect{
new Client(uuid: UUID.randomUUID(), name: "client #$it", date: LocalDate.now().plusDays(-it))
}
def container = new BeanItemContainer<Client>(Client, clientList)
def grid = new Grid(container)
grid.with{
setSizeFull()
// configure the columns
removeAllColumns()
[name: "Client name", date: "Start date"].each{ k,v ->
def col = addColumn(k)
col.setHeaderCaption(v)
}
}
setContent(grid)
}
}
Upvotes: 0