【问题标题】:Vaadin - Table column orderVaadin - 表格列顺序
【发布时间】:2013-10-22 21:39:43
【问题描述】:

任何人都知道如何/或有可能 - 创建一个具有特定列顺序的表格;保存之前的配置顺序 - 数据库中的示例, 并在特定视图上上传?我也想知道如何从 POJOS 类 - bean 中生成这些列标题和内容。

有什么好主意吗?

【问题讨论】:

    标签: vaadin multiple-columns pojo


    【解决方案1】:

    setVisibleColumns

    Table::setVisibleColumns 有双重职责:

    • 控制哪些列可见,
    • 设置列的显示顺序。

    致电Table::getVisibleColumns查看当前订单。

    文档

    这在以下有很好的描述:

    示例代码

    基本上,您需要这样的代码来控制列顺序并将 bean 实例列表设置为数据源。

    代码未经测试,只是一个演示。适用于 Vaadin 6,但我猜与 Vaadin 7 相比没有显着变化。

    table = new Table();
    
    // Wrap your beans collection into vaadin data container. There are many
    // types of them , check Book of Vaadin.    
    BeanItemContainer<Bean> container = new BeanItemContainer<Bean>(Bean.class)
    container.addBean(new Bean());
    
    // Set collection of your beans as data source. 
    // Columns will be created for each property, vaadin uses reflection.
    table.setContainerDataSource( container );
    
    // You can select to display only properties you want, not all. 
    // Order matters. You can get columns list from any source - for example
    // store in your DB.
    table.setVisibleColumns( new Object[] {"prop1", "prop2"} );
    
    // You can set column headers (by default vaadin will set them same as bean 
    // properties names). Order matters, should match above order.
    table.setColumnHeaders( new String[] {"Property 1", "Property2"} );
    

    【讨论】:

    • 如果您允许用户对列重新排序,则必须实现某种保存按钮,为该用户存储自定义的列顺序,并在重新显示时考虑此顺序。跨度>
    【解决方案2】:

    Sergey MakarovThe answer 是正确的。此答案提供了更多信息。

    用户的重新排序

    您可以允许用户拖放表格中的列,以便在运行时对其重新排序。要启用此功能,请致电isColumnReorderingAllowed

    当此类重新排序事件发生时,您可以使用listener 得到通知。

    用户的重新排序仅持续此工作会话。如果您想在未来的工作会话中维护用户的顺序,您必须以某种方式保持他们想要的顺序并在再次实例化表时应用该顺序。

    失去订单

    如果您更换表格的数据源,您的列顺序将被重置。您可以获取更改前的当前订单,然后恢复。示例代码如下。

    // Visible status & ordering.
    java.lang.Object[] visibleColumns = this.exampleTable.getVisibleColumns();
    
    // ------|  Fresh Data  |--------------
    // Put fresh data into the Table.
    this.exampleTable.setContainerDataSource( freshBeanItemContainer );
    
    // ------|  Restore Config  |--------------
    // Visible status & ordering.
    this.exampleTable.setVisibleColumns( visibleColumns ); // Assumes the table has the same properties.
    

    顺便说一句,与可见性和顺序一样,折叠也将使用新数据重置。您可能想要跟踪和恢复列折叠以及排序。

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多