t_sologub
t_sologub

Reputation: 893

Deselect selected row in Vaadin Grid

Can somebody give me a hint because I am stucked. And I haven't found the appropriate solution for my problem.

I have a Grid, with 1-3 rows. I click on the row -> the row is selected.

I want to have this row to be deselected after I click somewhere else (outside this row) but inside the grid.

Here is simple screenshot, to help you visialize it better.

enter image description here

What kind of listener should I use for this case? I have tried ItemClickListener -> haven't helped.

Upvotes: 2

Views: 5660

Answers (2)

RamanaMuttana
RamanaMuttana

Reputation: 491

gridLayout.asSingleSelect().addSelectionListener(e->{
    if(e.getFirstSelectedItem().isPresent()) {
          system.out.println("selected");
        }else {
         // when deselected make some actions here
            system.out.println("deSelected");
        }
});

Selection: If you click on the same row of data for the first time , it will print selected ,since there are values present on what you clicked .

Deselection: Now try to click on the same selected row for the second time , now it will go to deselection mode. since there are no values when you deselected.Now no need to click anywhere on the grid ,you can click on the same row of data for two times for the selection and deselection.

Upvotes: 1

Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

Try putting your grid in a separate layout and add LayoutClickListener to it:

gridLayout.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
    @Override
    public void layoutClick(LayoutEvents.LayoutClickEvent event) {
        if(grid.getSelectedRow() != null) {
            grid.deselectAll();
        }
    }
});

Upvotes: 1

Related Questions