【问题标题】:How to get clicked row number in table in Vaadin?如何在 Vaadin 的表格中获得点击的行号?
【发布时间】:2013-12-09 07:21:23
【问题描述】:

我需要获取用户点击了哪个行号来处理我的数据库操作。

try {
        for (int i = 1; i < dbManager.getLastTopicId() + 1; i++) {

            Integer itemId = new Integer(i);
            String topicTitle = dbManager.getTitle(Integer.toString(i));
            String topicText = dbManager.getText(Integer.toString(i));
            String topicAuthor = dbManager.getAuthor(Integer.toString(i));

            topicList.addItem(new Object[] {topicTitle, topicText, topicAuthor},
                    itemId);
        }
    } catch (UnsupportedOperationException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

这里我将数据加载到 topicList 表中。 我也用topicList.addValueChangeListener(new Property.ValueChangeListener() { public void valueChange(ValueChangeEvent event) 处理用户点击。但是我需要从那个监听器中获取用户点击了哪个行号。我已经尝试过topicList.getValue() 和其他东西,但没有一个会返回我的行号。 谢谢

【问题讨论】:

    标签: vaadin


    【解决方案1】:

    您可以通过添加ValueChangeListenerItemClickListener 来获取您的行ID。 注意:ItemClickListener 不会响应键盘向上或向下箭头,只有 ValueChangeListener 会。

    通过 EventObject,您可以访问项目 ID。

    Table table = new Table();
    table.setImmediate(true);
    table.setSelectable(true);
    table.addListener(new ValueChangeListener() { // With ValueChangeListener respons to keyboard input as well
    
        public void valueChange(ValueChangeEvent event) {
            getMainWindow().showNotification("ValueChange: ItemId: " + event.getProperty().getValue());
        }
    });
    table.addListener(new ItemClickListener() { // With ItemClickListener
    
        public void itemClick(ItemClickEvent event) {
            getMainWindow().showNotification("ItemClick: ItemId" + event.getItemId());
        }
    });
    

    在 Vaadin7 中,方法名称已更改为:

    table.addValueChangeListener();
    table.addItemClickListener();
    

    【讨论】:

    • 注意:如果一个表启用了多选,那么值的变化可以/将是一个集合。
    【解决方案2】:

    如果您只想跟踪点击次数,可以使用addItemClickListener()ItemClickListener 添加到表中。请参阅此answer

    如果您想读取表格的值,可能使用多选,您可以使用 ValueChangeListenergetValue() 读取 ID

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多