Select-all shortcut (Ctrl-A) in Vaadin Table?

2019-08-05 09:28发布

I have the following snippet in my UI Builder code:

table.addShortcutListener(new ShortcutListener("Select all", null, KeyCode.A, ModifierKey.CTRL) {

  @Override
  public void handleAction(Object sender, Object target) {
    AbstractSelect t = (AbstractSelect) target;
    if (t.isMultiSelect()) {
      t.setValue(t.getItemIds());
    }
  }
});
return table;

This allows to press Ctrl+A to select all items in a table. This usually works the first time I load a view until I make one of the tables invisible (setVisible(false)). After making the tables visible again, it no longer works (not even on reloading the page) and I get the following console output whenever I press Ctrl+A:

WARNING: Ignoring action for disabled connector c.b.a.web.ui.builder.table.TranslatedHeaderTable
Nov 03, 2014 11:15:00 AM com.vaadin.event.ConnectorActionManager handleAction

What is wrong with my code? How would I achieve my goal?

2条回答
Viruses.
2楼-- · 2019-08-05 10:16

I'd suggest this modification, works fine for me (i suppose comp is the Vaadin Table)

comp.addShortcutListener(new ShortcutListener("Select all", null, KeyCode.A, ModifierKey.CTRL) {

    private static final long serialVersionUID = 1L;

    @Override
    public void handleAction(Object sender, Object target) {
        if (comp.isMultiSelect()) {
            comp.setValue(comp.getItemIds());
        }
    }
});

The problem might be, while testing locally, the failure of serialization of some component, so a static reference to comp (you'll need to make it final) and a defaut UID should do the trick. tested multiple times and the error never occured. Cheers.

EDIT

I understood that the problem occured when making invisible and then visible the table. It came to my mind just now that you could have tried CTRL+A on an invisible Table: if this is the case so it's correct, when a Component is made invisible every listener is put in "Standby" until you make it visible again. So for me:

  • setVisible(false);
  • setVisible(true);
  • "CTRL+A"

works, while

  • setVisible(false);
  • "CTRL+A"

gives me

nov 03, 2014 2:19:34 PM com.vaadin.event.ConnectorActionManager handleAction
WARNING: Ignoring action for disabled connector com.vaadin.ui.Table

It's meant to be this way, nothing wrong in your code, you have to change your functionality and do not have a "CTRL+A" on an invisible Table (which seems a bad thing to do imho). On the other hand you could overwrite the setVisible method but I discourage it. Cheers.

查看更多
混吃等死
3楼-- · 2019-08-05 10:33

It seems there are some problems with the Table implementation of the Action.Notifier interface. In this Vaadin Forum Post, the Vaadin Devs suggest to add the ShortcutListener not to the Table itself but to a Panel that the Table is enclosed within.

My new implementation:

private void addCtrlAHandler(final AbstractSelect table) {
    Panel panelEnclosingTable = table.findAncestor(Panel.class);
    Preconditions.checkArgument(panelEnclosingTable != null, "Table is not enclosed in a panel; cannot add shortcut handlers");

    panelEnclosingTable.addShortcutListener(new ShortcutListener("Select all", null, KeyCode.A, ModifierKey.CTRL) {

        @Override
        public void handleAction(Object sender, Object target) {
            if (table.isMultiSelect()) {
                table.setValue(table.getItemIds());
            }
        }
    });
}

With this workaround, I get the expected behavior.

查看更多
登录 后发表回答