如何在vaadin中向表格的单元格添加单击侦听器?

时间:2012-07-23 11:15:29

标签: vaadin cell clicklistener

我在vaadin中创建了一个多选表,但是当我单击它选择所有行时,我不能只选择该表中的单元格。

有没有办法只选择一行的单元格?

2 个答案:

答案 0 :(得分:2)

问题的标题并未反映出

中的实际问题
  

有没有办法只选择一行的单元格?

没有。 Vaadin表的重点是以表格形式反映数据。将表设置为可选择使用表

跟踪所选行的itemIds

但是,删除监听器可能会很棘手。

或者,您可能希望简单地在ColumnGenerator中生成组件并自行跟踪所选单元格。

最终,这里的方法实际上取决于你想要实现的目标。

答案 1 :(得分:1)

这取决于你想要完成的事情。 (您的问题标题和您的问题详细信息正在解决两个不同的问题。)如果您想知道是否可以针对特定单元格并添加点击监听器,那么您当然可以:

//initial layout setup
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);

//Create a table and add a style to allow setting the row height in theme.
final Table table = new Table();
table.addStyleName("components-inside");

//Define the names and data types of columns.
//The "default value" parameter is meaningless here.
table.addContainerProperty("Sum",            Label.class,     null);
table.addContainerProperty("Is Transferred", CheckBox.class,  null);
table.addContainerProperty("Comments",       TextField.class, null);
table.addContainerProperty("Details",        Button.class,    null);

//Add a few items in the table.
for (int i=0; i<100; i++) {
    // Create the fields for the current table row
    Label sumField = new Label(String.format(
                   "Sum is <b>$%04.2f</b><br/><i>(VAT incl.)</i>",
                   new Object[] {new Double(Math.random()*1000)}),
                               Label.CONTENT_XHTML);
    CheckBox transferredField = new CheckBox("is transferred");

    //Multiline text field. This required modifying the 
    //height of the table row.
    TextField commentsField = new TextField();
    //commentsField.setRows(3);

    //The Table item identifier for the row.
    Integer itemId = new Integer(i);

    //Create a button and handle its click. A Button does not
    //know the item it is contained in, so we have to store the
    //item ID as user-defined data.
    Button detailsField = new Button("show details");
    detailsField.setData(itemId);
    detailsField.addListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            // Get the item identifier from the user-defined data.
            Integer iid = (Integer)event.getButton().getData();
            Notification.show("Link " +
                              iid.intValue() + " clicked.");
        } 
    });
    detailsField.addStyleName("link");

    //Create the table row.
    table.addItem(new Object[] {sumField, transferredField,
                                commentsField, detailsField},
                  itemId);
}

//Show just three rows because they are so high.
table.setPageLength(3);

layout.addComponent(table);

检查the documentation可能是有益的。