如何在Vaadin中添加Grid中的按钮

时间:2016-03-29 08:27:45

标签: vaadin vaadin7

您好我尝试在vaadin中添加一个网格按钮,但它会在按钮对象上打印引用。

Grid statementEnquiriesList = new Grid();
statementEnquiriesList.addColumn("", Button.class);
        statementEnquiriesList.addColumn("DATE/TIME", String.class);
        statementEnquiriesList.addColumn("TRANSACTION ID", String.class);
        statementEnquiriesList.addColumn("FROM", String.class);

// historyList is an array object
for (int i = 0; i < historyList.size(); i++)
{
    HistoryList recordObj = historyList.get(i);
    Button addBtn = new Button();
    addBtn.setCaption("Add");
    statementEnquiriesList.addRow(addBtn , recordObj.getDate(), recordObj.getTransactionId(), recordObj.getFrom());
}

我如何打印&#34;添加&#34;这个标题

enter image description here

2 个答案:

答案 0 :(得分:7)

您无法在Vaadin 7中直接使用网格中的组件。您必须使用ButtonRenderer来呈现按钮

RendererClickListener ownerClickListener = new RendererClickListener() {

    private static final long serialVersionUID = 1L;

    @Override
    public void click(RendererClickEvent event) {
        //Someone clicked button
    }
};
ButtonRenderer ownerRenderer = new ButtonRenderer(ownerClickListener, "");
grid.getColumn("ownerName").setRenderer(ownerRenderer);

但您可以在Vaadin 8中使用组件,请参阅Grid Components in Vaadin 8

我使用Vaadin 7并且ButtonRenderer对我不满意,因为没有办法将FontIcon添加到按钮,并且无法将其作为HTML插入。 相反,我使用了component renderer addon。以下是我如何使用它:

Grid grid = new Grid();
BeanItemContainer<EventChange> dataSource = //... primary data source
GeneratedPropertyContainer dataSource2 = new GeneratedPropertyContainer(dataSource);
grid.setContainerDataSource(dataSource2);
dataSource2.addGeneratedProperty("ownerWithButton", new PropertyValueGenerator<Component>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getValue(Item item, Object itemId, Object propertyId) {
            ClickListener ownerClickListener = new ClickListener() {

                private static final long serialVersionUID = 1L;
                @Override
                public void buttonClick(ClickEvent event) {
                    // do something, user clicked button for itemId
                }
            };
            Button button = new Button(FontAwesome.USER);
            button.addClickListener(ownerClickListener);
            return button;
        }

        @Override
        public Class<Component> getType() {
            return Component.class;
        }
    });
grid.setColumns("ownerWithButton", /*and rest of your columns*/);
grid.getColumn("ownerWithButton").setRenderer( new ComponentRenderer());

答案 1 :(得分:6)

Vaadin 8.1 - 网格中的组件

Vaadin 8.1现在有一个内置的ComponentRenderer,用于在网格中显示按钮或其他组件,包括您自己的自定义组件。

What's New页面上查看第一项“网格中的组件”。

示例:向网格添加标签。

grid.addColumn(
    person -> new Label( person.getFullName() ) ,
    new ComponentRenderer()
).setCaption( "Full Name" )