Vaadin 8 Grid approach by ListDataProvider

For Vaadin 8 Grid we can use a List Data Provioder

But first  why to use List Provider.?


Instead of directly assigning the item collection as the items that a component should be using, we can instead create a ListDataProvider that contains the items. One list data provider instance can be shared between different components to make them show the same data. The instance can be further configured to filter out some of the items or to present them in a specific order.
For components like Grid that can be separately configured to sort data in a specific way, the sorting configured in the data provider is only used as a fallback. The fallback is used if no sorting is defined through the component and to define the order between items that are considered to be the same according to the component’s sorting. All components will automatically update themselves when the sorting of the data provider is changed.
We can write a code for ListDataProvider as follows :-
First of all the Grid 
Going through the First post I am creating a Grid of type Company(our Pojo class)
public class TestGrid extends CustomComponent {
    private static final long serialVersionUID = 1L;
    private HorizontalLayout hlytbase;
    protected Grid<Company> menugrid;
    private List<Company> testitems;
    private ListDataProvider<Company> provider; --> Data provider of Pojo

    public TestGrid() {
        init();
    }
    protected void init() {
        initialize();
        setdata();
        buildLayout();
        setLayout();
    }
    private void setdata() {
        testitems.add(new Company(1,"Chocolates"));
        testitems.add(new Company(2, "Icecream"));
        testitems.add(new Company(3, "Pizza"));
        testitems.add(new Company(4, "Sandwhich"));
        testitems.add(new Company(5, "Burger"));
        menugrid.addColumn(Company::getSerialNo).setCaption("sr no");
        menugrid.addColumn(Company::getName).setCaption("Menu");
    }

    private void initialize() {
        menugrid = new Grid<>();
        testitems = new ArrayList<>();
        provider = new ListDataProvider<>(testitems);  --> the Collection as Arguement
        hlytbase = new HorizontalLayout();

    }
       private void buildLayout() {
        hlytbase.setMargin(true);
        hlytbase.setSizeFull();
        menugrid.setDataProvider(provider);
    }
    private void setLayout() {
        hlytbase.addComponents(menugrid);
        setCompositionRoot(hlytbase);
    }
what more can we do with the ListDataProvider say you have Created a Combobox according to It we can sort the whole Grid by either a serial no or with name 
dataProvider.setSortOrder(Person::getName,
    SortDirection.DESCENDING);
The above Code you can feed in the events of Button or combo box in Vaadin.
SO we can set in mind that we dont have to look up upon any particular Items just do the Operations or modifications in the provider the changes will be applied.

Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another