【问题标题】:How to add a click Listener to a table's cell in vaadin?如何在 vaadin 中将点击监听器添加到表格的单元格?
【发布时间】:2016-11-28 23:41:17
【问题描述】:

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

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

【问题讨论】:

    标签: vaadin cell clicklistener


    【解决方案1】:

    问题的标题并不反映里面的实际问题

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

    没有。 Vaadin 表的全部意义在于以表格形式反映 数据。将表设置为可选择的会跟踪表中所选行的 itemIds

    可能能够模拟通过在表格中使用ColumnGenerator 来选择单元格,并向生成的组件添加侦听器。然而,移除监听器可能会很棘手。

    或者,您可能希望简单地在 GridLayout 中生成组件并自己跟踪选定的单元格。

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

    【讨论】:

    • 感谢您的回答。我搜索了 ColumnGenerator 并编写了一个代码,但它不起作用。当我单击一个单元格时,我正在尝试做一个表格(7X24),该行中的其他单元格将不会被选中。如果你能给我一个简短的例子,我会很高兴。
    • @Hasan : 你试过让表 setSelectable(false) 吗?
    • 不,我没有。代码在这里manashk.com/java。程序只创建表格,不加载内容。
    • 表反映了容器(就像一个表模型)。由于您没有添加任何行/项目,因此没有内容。查看 Vaadin 论坛上的这篇文章,了解 ColumnGenerator vaadin.com/forum/-/message_boards/view_message/1602552 的工作示例
    • 亲爱的查尔斯,此代码不起作用。类型异常报告消息:描述服务器遇到内部错误(),阻止它完成此请求异常:javax.servlet.ServletException:java.lang.ClassCastException: com.example.testwindow.TestWindow 不能转换为 com.vaadin.Application com.vaadin.terminal.gwt.server.AbstractApplicationServlet.handleServiceException(AbstractApplicationServlet.java:1004) com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service( AbstractApplicationServlet.java:548) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    【解决方案2】:

    这取决于您要完成的工作。 (您的问题标题和问题详细信息解决了两个不同的问题。)如果您想知道是否可以定位特定单元格并为其添加点击侦听器,那么可以,当然可以:

    //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 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多