【问题标题】:How to order Vaadin Table Rows?如何订购 Vaadin 表格行?
【发布时间】:2012-11-01 16:22:32
【问题描述】:

我有一个表 phases,它的值引用了 repository 表。在存储库中,每个值都有一个订单号。

通常在 SQL 中,我会加入它们,然后按顺序列排序。但是,如果我想在 Vaadin 中执行此操作,我必须使用 FreeFormQuery 进行此连接。

问题是我希望允许用户更改表 phases 中的值。所以我需要实现FreeFormQueryDelegate。有没有办法通过使用标准 TableQuery 而不是 FreeFormQuery 来完成这项工作?

也许手动移动表格中的行?

【问题讨论】:

    标签: java sql vaadin


    【解决方案1】:

    Vaadin 为表格提供了一些排序选项。

    您可以对一个特定的 propertyId 进行排序,也可以对一组属性进行排序。

    // sort one container property Id
    table.setSortContainerPropertyId(propertyId);
    table.setSortAscending(ascending);
    table.sort();
    
    // sort several property Ids
    table.sort(propertyId[], ascending[]);
    

    注意:上面的代码不是正确的代码,而是显示方法体。

    【讨论】:

    • 仅供参考:这仅适用于普通列名,如果您的 order by 子句具有任何类型的复杂表达式(例如 ORDER BY column1/column2),则需要使用不同的方法
    【解决方案2】:

    我使用以下代码重新排列 vaadin 表格行,当单击“上移”按钮时,同时选择表格行。

    Object selectedItemId = fieldTable.getValue();
            if(selectedItemId != null){
                List<Object> items = new ArrayList<>(fieldTable.getItemIds());
                int index = items.indexOf(selectedItemId);
    
                if(index < 1){
                    LOGGER.info("Selected Filed is at the top, cannot move up further");
                    return;
                }
    
                //Rearrange itemids for new order
                Object upperRow= items.set(index - 1, selectedItemId);
                items.set(index, upperRow);
    
                items.forEach(itemId -> {
                    //Gets the properties of old table item
                    Item item = fieldTable.getItem(itemId);
                    String name = (String) item.getItemProperty(Constants.PROPERTYID_FIELD_NAME).getValue();
                    Button toggleBtn = (Button) item.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).getValue();
    
                    //Remove the old table item
                    fieldTable.removeItem(itemId);
    
                    //Insert old table item into the new position index
                    Item newItem = fieldTable.addItem(itemId);
                    newItem.getItemProperty(Constants.PROPERTYID_FIELD_NAME).setValue(name);
                    newItem.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).setValue(toggleBtn);
    
                    int position = items.indexOf(itemId);
                    if(position == index - 1) {
                        fieldTable.select(itemId);
                    }
                });
            }
    

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多