Search in sources :

Example 1 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class LazyLoadingPage method createCallbackDataProvider.

private void createCallbackDataProvider() {
    addTitle("CallbackDataProvider");
    ComboBox<String> comboBox = new ComboBox<>();
    comboBox.setId("callback-dataprovider");
    AtomicInteger sizeRequestCount = new AtomicInteger(0);
    Span sizeRequestCountSpan = new Span("0");
    sizeRequestCountSpan.setId("callback-dataprovider-size-request-count");
    CallbackDataProvider<String, String> dataProvider = new CallbackDataProvider<String, String>(query -> IntStream.range(query.getOffset(), query.getOffset() + query.getLimit()).mapToObj(i -> "Item " + i), query -> {
        sizeRequestCountSpan.setText(String.valueOf(sizeRequestCount.incrementAndGet()));
        return 210;
    });
    comboBox.setDataProvider(dataProvider);
    add(comboBox, sizeRequestCountSpan);
}
Also used : IntStream(java.util.stream.IntStream) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Collectors(java.util.stream.Collectors) H4(com.vaadin.flow.component.html.H4) Serializable(java.io.Serializable) Route(com.vaadin.flow.router.Route) ComboBoxInATemplate(com.vaadin.flow.component.combobox.test.template.ComboBoxInATemplate) List(java.util.List) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataProvider(com.vaadin.flow.data.provider.DataProvider) Element(com.vaadin.flow.dom.Element) Collections(java.util.Collections) Paragraph(com.vaadin.flow.component.html.Paragraph) Span(com.vaadin.flow.component.html.Span) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Span(com.vaadin.flow.component.html.Span) CallbackDataProvider(com.vaadin.flow.data.provider.CallbackDataProvider)

Example 2 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class GridUpdateDataProviderPage method createBasicGrid.

private void createBasicGrid() {
    Grid<String> grid = new Grid<>();
    grid.setId("basic-grid");
    DataProvider<String, ?> orig = DataProvider.fromCallbacks(query -> IntStream.range(query.getOffset(), query.getOffset() + query.getLimit()).mapToObj(Integer::toString), query -> 10000);
    DataProvider<String, ?> updated = DataProvider.ofItems("foo", "foob", "fooba", "foobar");
    grid.setDataProvider(orig);
    grid.addColumn(i -> i).setHeader("text");
    grid.addColumn(i -> String.valueOf(i.length())).setHeader("length");
    NativeButton updateProvider = new NativeButton("Use another provider", event -> swapDataProviders(grid, orig, updated));
    updateProvider.setId("update-basic-provider");
    add(new H2("Basic grid"), grid, updateProvider);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) List(java.util.List) Grid(com.vaadin.flow.component.grid.Grid) DataProvider(com.vaadin.flow.data.provider.DataProvider) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) H2(com.vaadin.flow.component.html.H2) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) Grid(com.vaadin.flow.component.grid.Grid) H2(com.vaadin.flow.component.html.H2)

Example 3 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class RadioButtonGroup method reset.

@SuppressWarnings("unchecked")
private void reset() {
    keyMapper.removeAll();
    clear();
    synchronized (dataProvider) {
        // Cache helper component before removal
        Component helperComponent = getHelperComponent();
        // Remove all known children (doesn't remove client-side-only
        // children such as the label)
        getChildren().forEach(this::remove);
        // reinsert helper component
        setHelperComponent(helperComponent);
        final AtomicInteger itemCounter = new AtomicInteger(0);
        getDataProvider().fetch(DataViewUtils.getQuery(this)).map(item -> createRadioButton((T) item)).forEach(component -> {
            add((Component) component);
            itemCounter.incrementAndGet();
        });
        lastFetchedDataSize = itemCounter.get();
        // so as to avoid multiple beforeClientResponses.
        if (sizeRequest == null) {
            sizeRequest = ui -> {
                fireSizeEvent();
                sizeRequest = null;
            };
            // Size event is fired before client response so as to avoid
            // multiple size change events during server round trips
            runBeforeClientResponse(sizeRequest);
        }
    }
}
Also used : DataViewUtils(com.vaadin.flow.data.provider.DataViewUtils) KeyMapper(com.vaadin.flow.data.provider.KeyMapper) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Component(com.vaadin.flow.component.Component) RadioButtonGroupDataView(com.vaadin.flow.component.radiobutton.dataview.RadioButtonGroupDataView) ItemLabelGenerator(com.vaadin.flow.component.ItemLabelGenerator) NpmPackage(com.vaadin.flow.component.dependency.NpmPackage) ComponentUtil(com.vaadin.flow.component.ComponentUtil) Registration(com.vaadin.flow.shared.Registration) HasListDataView(com.vaadin.flow.data.provider.HasListDataView) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) RadioButtonGroupListDataView(com.vaadin.flow.component.radiobutton.dataview.RadioButtonGroupListDataView) HasDataView(com.vaadin.flow.data.provider.HasDataView) SingleSelect(com.vaadin.flow.data.selection.SingleSelect) AtomicReference(java.util.concurrent.atomic.AtomicReference) HasLabel(com.vaadin.flow.component.HasLabel) ItemCountChangeEvent(com.vaadin.flow.data.provider.ItemCountChangeEvent) HasValidation(com.vaadin.flow.component.HasValidation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataProvider(com.vaadin.flow.data.provider.DataProvider) IdentifierProvider(com.vaadin.flow.data.provider.IdentifierProvider) PropertyChangeEvent(com.vaadin.flow.dom.PropertyChangeEvent) UI(com.vaadin.flow.component.UI) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) AttachEvent(com.vaadin.flow.component.AttachEvent) HasSize(com.vaadin.flow.component.HasSize) Query(com.vaadin.flow.data.provider.Query) DataChangeEvent(com.vaadin.flow.data.provider.DataChangeEvent) Collection(java.util.Collection) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Serializable(java.io.Serializable) Objects(java.util.Objects) Stream(java.util.stream.Stream) DetachEvent(com.vaadin.flow.component.DetachEvent) HasItemComponents(com.vaadin.flow.data.binder.HasItemComponents) DataProviderWrapper(com.vaadin.flow.data.provider.DataProviderWrapper) Optional(java.util.Optional) HasHelper(com.vaadin.flow.component.HasHelper) TextRenderer(com.vaadin.flow.data.renderer.TextRenderer) InMemoryDataProvider(com.vaadin.flow.data.provider.InMemoryDataProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Component(com.vaadin.flow.component.Component)

Example 4 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class CheckboxGroup method reset.

@SuppressWarnings("unchecked")
private void reset() {
    keyMapper.removeAll();
    clear();
    synchronized (dataProvider) {
        // Cache helper component before removal
        Component helperComponent = getHelperComponent();
        // Remove all known children (doesn't remove client-side-only
        // children such as the label)
        getChildren().forEach(this::remove);
        // reinsert helper component
        // see https://github.com/vaadin/vaadin-checkbox/issues/191
        setHelperComponent(helperComponent);
        final AtomicInteger itemCounter = new AtomicInteger(0);
        getDataProvider().fetch(DataViewUtils.getQuery(this)).map(item -> createCheckBox((T) item)).forEach(component -> {
            add((Component) component);
            itemCounter.incrementAndGet();
        });
        lastFetchedDataSize = itemCounter.get();
        // so as to avoid multiple beforeClientResponses.
        if (sizeRequest == null) {
            sizeRequest = ui -> {
                fireSizeEvent();
                sizeRequest = null;
            };
            // Size event is fired before client response so as to avoid
            // multiple size change events during server round trips
            runBeforeClientResponse(sizeRequest);
        }
    }
}
Also used : DataViewUtils(com.vaadin.flow.data.provider.DataViewUtils) KeyMapper(com.vaadin.flow.data.provider.KeyMapper) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) MultiSelect(com.vaadin.flow.data.selection.MultiSelect) Component(com.vaadin.flow.component.Component) ItemLabelGenerator(com.vaadin.flow.component.ItemLabelGenerator) ComponentUtil(com.vaadin.flow.component.ComponentUtil) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) HasListDataView(com.vaadin.flow.data.provider.HasListDataView) JsonArray(elemental.json.JsonArray) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) HasDataView(com.vaadin.flow.data.provider.HasDataView) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) HasLabel(com.vaadin.flow.component.HasLabel) ItemCountChangeEvent(com.vaadin.flow.data.provider.ItemCountChangeEvent) CheckboxGroupListDataView(com.vaadin.flow.component.checkbox.dataview.CheckboxGroupListDataView) HasValidation(com.vaadin.flow.component.HasValidation) MultiSelectionEvent(com.vaadin.flow.data.selection.MultiSelectionEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataProvider(com.vaadin.flow.data.provider.DataProvider) IdentifierProvider(com.vaadin.flow.data.provider.IdentifierProvider) MultiSelectionListener(com.vaadin.flow.data.selection.MultiSelectionListener) PropertyChangeEvent(com.vaadin.flow.dom.PropertyChangeEvent) UI(com.vaadin.flow.component.UI) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) HasSize(com.vaadin.flow.component.HasSize) Query(com.vaadin.flow.data.provider.Query) DataChangeEvent(com.vaadin.flow.data.provider.DataChangeEvent) Collection(java.util.Collection) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Stream(java.util.stream.Stream) HasItemComponents(com.vaadin.flow.data.binder.HasItemComponents) DataProviderWrapper(com.vaadin.flow.data.provider.DataProviderWrapper) Optional(java.util.Optional) HasHelper(com.vaadin.flow.component.HasHelper) CheckboxGroupDataView(com.vaadin.flow.component.checkbox.dataview.CheckboxGroupDataView) Collections(java.util.Collections) InMemoryDataProvider(com.vaadin.flow.data.provider.InMemoryDataProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Component(com.vaadin.flow.component.Component)

Example 5 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class IronListView method createDisabledStringsList.

private void createDisabledStringsList() {
    IronList<String> list = new IronList<>();
    list.setHeight("400px");
    list.getStyle().set("border", "1px solid lightgray");
    Div removalResult = new Div();
    removalResult.setId("disabled-removal-result");
    DataProvider<String, ?> dataProvider = DataProvider.fromCallbacks(query -> queryStringsFromDatabase(query), query -> countStringsFromDatabase(query));
    list.setDataProvider(dataProvider);
    // Disable the list so that scrolling still works but events are not
    // handled
    list.setEnabled(false);
    /*
         * The name of the event handlers defined at 'on-click' are used inside
         * the 'withEventHandler' calls.
         */
    list.setRenderer(TemplateRenderer.<String>of("<div style='display:flex; justify-content:space-between; padding:10px;'>" + "<div style='flex-grow:1'>[[item.name]]</div>" + "<div><button on-click='remove' style='color:red'>X</button></div>" + "<div>").withProperty("name", ValueProvider.identity()).withEventHandler("remove", item -> {
        removalResult.setText(item);
    }));
    NativeButton switchEnabled = new NativeButton("Switch enabled state", event -> list.setEnabled(!list.isEnabled()));
    list.setId("disabled-list-with-templates");
    switchEnabled.setId("switch-enabled-state-string-list");
    addCard("Using templates", "Using disabled list with templates", new Label("Rank up/down your favorite Lord of the Rings characters"), list, removalResult, switchEnabled);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Image(com.vaadin.flow.component.html.Image) Component(com.vaadin.flow.component.Component) ValueProvider(com.vaadin.flow.function.ValueProvider) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) SecureRandom(java.security.SecureRandom) HashSet(java.util.HashSet) Faker(com.github.javafaker.Faker) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) Query(com.vaadin.flow.data.provider.Query) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Set(java.util.Set) H2(com.vaadin.flow.component.html.H2) Serializable(java.io.Serializable) List(java.util.List) Stream(java.util.stream.Stream) IronList(com.vaadin.flow.component.ironlist.IronList) Collections(java.util.Collections) NativeButton(com.vaadin.flow.component.html.NativeButton) Label(com.vaadin.flow.component.html.Label) IronList(com.vaadin.flow.component.ironlist.IronList)

Aggregations

DataProvider (com.vaadin.flow.data.provider.DataProvider)11 Query (com.vaadin.flow.data.provider.Query)9 Div (com.vaadin.flow.component.html.Div)8 NativeButton (com.vaadin.flow.component.html.NativeButton)8 Route (com.vaadin.flow.router.Route)8 List (java.util.List)8 Stream (java.util.stream.Stream)8 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)7 Arrays (java.util.Arrays)7 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Component (com.vaadin.flow.component.Component)4 Serializable (java.io.Serializable)4 Collections (java.util.Collections)4 ComponentUtil (com.vaadin.flow.component.ComponentUtil)3 HasHelper (com.vaadin.flow.component.HasHelper)3 HasLabel (com.vaadin.flow.component.HasLabel)3