Vaadin: Grid duplicates data

I wrote Grid, as shown in example.

public class MainView extends VerticalLayout {

    private final UserService userService;
    private Label label;
    private Grid<UserDto> grid;

    public MainView(UserService userService) {
        this.userService = userService;
        initComponents(userService);
    }

    private void initComponents(UserService userService) {
        initMainGrid(userService);
    }

    private void initMainGrid(UserService userService) {
        grid = new Grid<>(UserDto.class);

        grid.setItems(userService.getAll());

        grid.addColumn(UserDto::getId).setHeader("ID");
        grid.addColumn(UserDto::getUsername).setHeader("Имя пользователя");
        grid.addColumn(UserDto::getPassword).setHeader("Пароль");
        grid.addColumn(UserDto::getEmail).setHeader("email");
        grid.addColumn(UserDto::getPhone).setHeader("Телефон");
        grid.addColumn(UserDto::getActive).setHeader("Активен");

        add(grid);
    }
}

But data duplicates:

введите сюда описание изображения

As we see, data shows two times.

Please advice, what wrong? How to fix it?

Upvotes: 3

Views: 733

Answers (2)

kscherrer
kscherrer

Reputation: 5766

Small addition to the correct answer of @TatuLund

You can also initialize the grid with an additional boolean parameter which will prevent the automatic generation of columns. This way you can still use property names when adding columns (OP does not use this, but if he did then this would be very useful information).

Grid<UserDto> grid = new Grid<>(UserDto.class, false);
grid.addColumn("id"); // this would not be possible witout passing the class in constructor

See the API Documentation of this Grid constructor for reference

Upvotes: 5

Tatu Lund
Tatu Lund

Reputation: 10633

Passing Class object to Grid constructor

This constructor grid = new Grid<>(UserDto.class); will automatically add columns according to setters and getters of UserDto class.

Not passing Class object to Grid constructor

If you want to configure columns using addColumn method, you need to instantiate Grid using grid = new Grid<>(); without passing UserDto.class.

Upvotes: 7

Related Questions