Jednoduché použití komponenty Grid ve Vaadinu

Grid je komponenta pro zobrazení tabulkových dat (ve formátu sloupců a řádek). Zde je ochutnávka toho, co grid umí: demo.vaadin.com/sampler/#ui/grid.

V tomto příspěvku si vytvoříme jednoduchý grid, který bude zobrazovat data lidí (jméno, příjmení a den narození). Kód můžete umístit do metody init třídy, která dědí z UI.

@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();

    // creating data
    List<Person> persons = Arrays.asList(
    		new Person("John", "Smith", LocalDate.of(2001, 3, 17)),
    		new Person("Marie", "Jones", LocalDate.of(1987, 11, 26)),
    		new Person("Dave", "Williams", LocalDate.of(1995, 1, 5))
    );

    // creating grid
    Grid<Person> grid = new Grid<>();
    grid.setItems(persons);
    grid.addColumn(Person::getName).setCaption("Name");
    grid.addColumn(Person::getSurname).setCaption("Surname");
    grid.addColumn(Person::getDateOfBirth).setCaption("Date of birth");
    grid.setHeightByRows(persons.size());

    layout.addComponent(grid);

    setContent(layout);
}

Person.java

public class Person implements Serializable {

	private String name;
	private String surname;
	private LocalDate dateOfBirth;
	
	public Person(String name, String surname, LocalDate dateOfBirth) {
		this.name = name;
		this.surname = surname;
		this.dateOfBirth = dateOfBirth;
	}

	// getters and setters
}

Výsledek:

vaadin_grid_basic

Pokud zakomentujete řádek grid.setHeightByRows(persons.size()), výsledek bude následující:

vaadin_grid_basic_02


 

Zdroj: vaadin.com/…/components-grid.html

Napsat komentář