From a4922d7da413c6e5b0202afacf2ecfe5e964f887 Mon Sep 17 00:00:00 2001 From: Felipe Lang Date: Tue, 5 Dec 2023 17:57:41 -0300 Subject: [PATCH] feat: add demo for empty grid label --- .../gridhelpers/EmptyGridLabelDemo.java | 100 ++++++++++++++++++ .../gridhelpers/GridHelpersDemoView.java | 1 + .../addons/gridhelpers/LazyTestData.java | 33 +++++- .../META-INF/resources/gridhelpers/styles.css | 10 ++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/flowingcode/vaadin/addons/gridhelpers/EmptyGridLabelDemo.java diff --git a/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/EmptyGridLabelDemo.java b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/EmptyGridLabelDemo.java new file mode 100644 index 0000000..6e84825 --- /dev/null +++ b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/EmptyGridLabelDemo.java @@ -0,0 +1,100 @@ +/*- + * #%L + * Grid Helpers Add-on + * %% + * Copyright (C) 2022 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +package com.flowingcode.vaadin.addons.gridhelpers; + +import com.flowingcode.vaadin.addons.demo.DemoSource; +import com.flowingcode.vaadin.addons.gridhelpers.LazyTestData.PersonFilter; +import com.vaadin.flow.component.Key; +import com.vaadin.flow.component.Text; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.formlayout.FormLayout; +import com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep; +import com.vaadin.flow.component.grid.Grid; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.H3; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.data.provider.DataProvider; +import com.vaadin.flow.data.provider.Query; +import com.vaadin.flow.router.PageTitle; +import com.vaadin.flow.router.Route; +import java.util.function.Function; + +@PageTitle("Empty Grid Label") +@DemoSource +@Route(value = "grid-helpers/empty-grid-label", layout = GridHelpersDemoView.class) +public class EmptyGridLabelDemo extends VerticalLayout { + + public EmptyGridLabelDemo() { + LazyTestData data = new LazyTestData(); + + setSpacing(false); + setPadding(false); + setSizeFull(); + + TextField firstName = new TextField("Firstname"); + TextField lastName = new TextField("Lastname"); + + Function, PersonFilter> filterBuilder = query -> { + PersonFilter filter = query.getFilter().orElse(new PersonFilter()); + filter.setFirstName(firstName.getValue()); + filter.setLastName(lastName.getValue()); + return filter; + }; + + DataProvider dataProvider = DataProvider.fromFilteringCallbacks( + query -> data.filter(query.getOffset(), query.getPageSize(), filterBuilder.apply(query)), + query -> data.count(filterBuilder.apply(query))); + + Grid grid = new Grid<>(); + grid.setDataProvider(dataProvider); + grid.addColumn(Person::getFirstName).setHeader("First name"); + grid.addColumn(Person::getLastName).setHeader("Last name"); + grid.addColumn(Person::getCountry).setHeader("Country"); + grid.setSizeFull(); + + Div emptyGridLabel = new Div(new Text("No records found")); + emptyGridLabel.setSizeFull(); + emptyGridLabel.addClassName("empty-grid-label"); + emptyGridLabel.setVisible(false); + GridHelper.setEmptyGridLabel(grid, emptyGridLabel); + + Button filterButton = new Button("Filter"); + filterButton.addClickShortcut(Key.ENTER); + filterButton.addClickListener(e -> dataProvider.refreshAll()); + + Div gridContainer = new Div(grid, emptyGridLabel); + gridContainer.getStyle().set("position", "relative"); + gridContainer.setSizeFull(); + + FormLayout filtersForm = new FormLayout(firstName, lastName); + filtersForm.setResponsiveSteps(new ResponsiveStep("0", 2)); + filtersForm.setWidthFull(); + + HorizontalLayout filters = new HorizontalLayout(filtersForm, filterButton); + filters.setAlignItems(Alignment.END); + filters.setWidthFull(); + filters.getStyle().set("padding-right", "4px"); + + add(filters, gridContainer); + } +} diff --git a/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelpersDemoView.java b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelpersDemoView.java index caebc7d..7a4d441 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelpersDemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/GridHelpersDemoView.java @@ -50,5 +50,6 @@ public GridHelpersDemoView() { addDemo(GetHeaderFooterDemo.class); addDemo(LazyMultiSelectionDemo.class); addDemo(CheckboxColumnDemo.class); + addDemo(EmptyGridLabelDemo.class); } } diff --git a/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/LazyTestData.java b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/LazyTestData.java index d9698cf..00d0e25 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/LazyTestData.java +++ b/src/test/java/com/flowingcode/vaadin/addons/gridhelpers/LazyTestData.java @@ -24,6 +24,9 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.StringUtils; class LazyTestData { @@ -52,15 +55,33 @@ public LazyTestData() { } public Stream filter(int offset, int pageSize) { + return filter(offset, pageSize, null); + } + + public Stream filter(int offset, int pageSize, PersonFilter filter) { int from = Math.max(0, offset); - int to = Math.min(data.size(), pageSize + offset); - return data.subList(from, to).stream(); + if (filter != null) { + return data.stream() + .filter( + item -> StringUtils.containsIgnoreCase(item.getFirstName(), filter.getFirstName())) + .filter(item -> StringUtils.containsIgnoreCase(item.getLastName(), filter.getLastName())) + .skip(from) + .limit(pageSize); + } + return data.stream().skip(from).limit(pageSize); } public int count() { return data.size(); } + public int count(PersonFilter filter) { + return (int) data.stream() + .filter(item -> StringUtils.containsIgnoreCase(item.getFirstName(), filter.getFirstName())) + .filter(item -> StringUtils.containsIgnoreCase(item.getLastName(), filter.getLastName())) + .count(); + } + private static String generateCountry() { String country = faker.address().country(); if (country.contains("South Georgia") || country.contains("Falkland")) { @@ -69,4 +90,12 @@ private static String generateCountry() { return country; } } + + @Getter + @Setter + public static class PersonFilter { + private String firstName; + private String lastName; + } + } diff --git a/src/test/resources/META-INF/resources/gridhelpers/styles.css b/src/test/resources/META-INF/resources/gridhelpers/styles.css index 9cb0808..c07adc3 100644 --- a/src/test/resources/META-INF/resources/gridhelpers/styles.css +++ b/src/test/resources/META-INF/resources/gridhelpers/styles.css @@ -24,3 +24,13 @@ label.label { font-weight: 500; line-height: 1; } + +.empty-grid-label { + position: absolute; + top: 0px; + left: 0px; + display: flex; + justify-content: center; + align-items: center; + font-size: 32px; +} \ No newline at end of file