Vaadin 8 on Grails 3
  • Introduction
  • Project setup
    • Environment setup
      • Unix based systems
      • Windows
    • Creating Project
      • Command line
      • IntelliJ IDEA
    • Plugin Configuration
      • UI class
      • URL mapping
      • Production mode
      • Async support
      • Themes
      • SASS compilation
      • Widgetset compilation
      • Servlet class
      • Spring component scan
      • UI provider
      • Open session in view
    • Clean up
    • Best Practices
  • Database
    • GORM
      • Create Domain Model
      • Transactions
      • LazyInitializationException
      • Open Session In View I.
      • Open Session In View II.
      • Table Container
    • Groovy SQL
      • Create Sql Bean
      • Execute SQLs
    • MyBatis
      • MyBatis Configuration
      • Reading Data with MyBatis
    • JdbcTemplate
      • Create JdbcTemplate Beans
      • Using JdbcTemplate
    • Clean Up When Using Alternatives
  • UI
Powered by GitBook
On this page
  • Step 1
  • Step 2
  • Step 3
  • Step 4

Was this helpful?

  1. Database
  2. GORM

Table Container

PreviousOpen Session In View II.NextGroovy SQL

Last updated 5 years ago

Was this helpful?

Example code is available on .

In this tutorial we will show how to display GORM objects in Vaadin Grid.

Step 1

Create a domain object Item with two String fields name and other.

package com.vaadinongrails

class Item {

    String name
    String other

    static constraints = {
    }
}

Step 2

Create few records of Item in database in BootStrap.groovy file.

class BootStrap {

    def init = { servletContext ->
        new Item(name: "Hi 1", other: "There").save(failOnError: true)
        new Item(name: "Hi 2", other: "There").save(failOnError: true)
        new Item(name: "Hi 3", other: "There").save(failOnError: true)
        new Item(name: "Hi 4", other: "There").save(failOnError: true)
    }
    def destroy = {
    }
}

Step 3

Create instance of Grid in Vaadin code. We need to do two things to see GORM objects nicely displayed in the grid:

  1. Create new BeanItemContainer and add GORM objects into it.

  2. GORM object contains more fields then we have defined and we have to pick what columns will be visible. Then we have to remove the others which are not supposed to be visible.

package app

import com.vaadin.data.util.BeanItemContainer
import com.vaadin.grails.ui.VaadinUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Grid
import com.vaadin.ui.UI
import com.vaadin.ui.VerticalLayout
import com.vaadinongrails.Item

@VaadinUI(path = '/')
class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        VerticalLayout layout = new VerticalLayout()
        layout.setMargin(true)

        Grid grid = new Grid()
        grid.setSelectionMode(Grid.SelectionMode.SINGLE)

        BeanItemContainer<Item> container = new BeanItemContainer<>(Item.class)
        List<Item> all = Item.findAll()
        container.addAll(all)
        grid.setContainerDataSource(container)

        grid.setColumnOrder("id", "name", "other")
        grid.removeColumn("attached")
        grid.removeColumn("metaClass")
        grid.removeColumn("properties")
        grid.removeColumn("version")
        grid.removeColumn("dirty")
        grid.removeColumn("dirtyPropertyNames")
        grid.removeColumn("errors")

        layout.addComponent(grid)

        setContent(layout)
    }
}

Step 4

Run application and see the table with GORM objects from database.

MVP running app
github.com/vaadin-on-grails3/gorm-vaadin-table