【问题标题】:Vaadin Grid and Grails Domain ClassVaadin 网格和 Grails 域类
【发布时间】:2015-12-11 03:16:55
【问题描述】:

晚上好。我在 Vaadin 网格中显示 Grails 域对象信息时遇到问题。这是我到目前为止所拥有的:

contenedorClientes = new BeanItemContainer<Cliente>(Cliente.class, Grails.get(ClientesService).obtenerClientes())    

gdClientes = new Grid()
gdClientes.containerDataSource = contenedorClientes

基本上,我正在做的是:首先,我创建一个 BeanItemContainer,然后我将其设置为将此容器配置为 Cliente 类型之一,并且我还设置了此容器的数据源,在本例中为Grails 服务的一种方法,它返回 Cliente 类型的对象列表。

然后,我实例化一个 Vaadin 网格并将其 containerDataSource 设置为之前创建的容器。

我遇到的主要问题是网格还显示来自 Cliente 扩展的域类的信息。这意味着还会显示诸如 Version、Dirty、Meta Class 等属性。我不希望这样,我只想显示我创建的 Domain 类中的数据。

这是域类:

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)

    }
}

我需要做什么才能只显示这个类中的信息,而不是它派生的超类中的信息?

另外,有人知道如何设置网格中列的呈现顺序吗?

提前感谢您的帮助。

【问题讨论】:

    标签: grails vaadin7 vaadin-grid


    【解决方案1】:

    Grid section in the Book of Vaadin.

    一般的方法是这样的:首先在设置容器后删除所有带有removeAllColumns 的列,然后继续添加实际的列配置。由于您控制了这一步,因此列的顺序自然而然(如果您真的只想重新排序 all 现有的,请使用 setColumnOrder)。

    这是该方法的一个独立示例:

    // 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)
        }
    }
    

    【讨论】:

      【解决方案2】:

      您想在添加项目之前从容器中删除未使用的列

                          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)
      

      【讨论】:

        【解决方案3】:

        我通过使用网格的方法 addColumns 并指定我想要显示的列来解决这个问题。无需删除列。

        【讨论】:

          猜你喜欢
          • 2022-08-18
          • 2014-10-19
          • 1970-01-01
          • 1970-01-01
          • 2015-07-19
          • 1970-01-01
          • 1970-01-01
          • 2019-04-22
          • 1970-01-01
          相关资源
          最近更新 更多