Search code examples
cuba-platform

Best way to intercept click event on table item


I'm using two tables, the first one contains "Teams", the second one "Team members" and is populating based on the first table selection. I'm also showing various stats depending on the selection, be it a team or a specific member. If no member is selected, team stats are showed, otherwise member stats are showed.

I'm using ItemChangeListeners on the tables to redraw the stats, but this prevents me to click on an already selected team to "deselect" a selected member from that team, since no event is triggered in that circumstance. As a solution I'm also using a ClickListener on the Team table, but it seems to work only if I click on the word (instead of working on the whole cell).

teamsTable.setClickListener("name", new Table.CellClickListener() {
        @Override
        public void onClick(Entity item, String columnId) {
            if (teamsDs.getItem() == item) {
                teamsDs.setItem(null);
                teamsDs.setItem((Team) item);
            } else {
                teamsDs.setItem((Team) item);
                teamsTable.setSelected((Team) item);
            }
        }
    });

Is there a better way to catch a click on a table cell? Or is there a better way to approach the problem altogether?


Solution

  • Since CUBA Table is a wrapper of Vaadin table you can use ItemClickListener from Vaadin with CUBA table:

    public class DemoScreen extends AbstractWindow {
        @Inject
        private Table<User> usersTable;
    
        @Override
        public void init(Map<String, Object> params) {
            super.init(params);
    
            com.vaadin.ui.Table vTable = usersTable.unwrap(com.vaadin.ui.Table.class);
            vTable.addItemClickListener((ItemClickEvent.ItemClickListener) event ->
                    showNotification("Item " + event.getItemId())
            );
        }
    }
    

    It will be fired each time you click on a table cell.