Skip to content

Commit

Permalink
feat: add demo for empty grid label
Browse files Browse the repository at this point in the history
  • Loading branch information
flang committed Dec 22, 2023
1 parent d3522a7 commit a4922d7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<Query<Person, PersonFilter>, PersonFilter> filterBuilder = query -> {
PersonFilter filter = query.getFilter().orElse(new PersonFilter());
filter.setFirstName(firstName.getValue());
filter.setLastName(lastName.getValue());
return filter;
};

DataProvider<Person, PersonFilter> dataProvider = DataProvider.fromFilteringCallbacks(
query -> data.filter(query.getOffset(), query.getPageSize(), filterBuilder.apply(query)),
query -> data.count(filterBuilder.apply(query)));

Grid<Person> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ public GridHelpersDemoView() {
addDemo(GetHeaderFooterDemo.class);
addDemo(LazyMultiSelectionDemo.class);
addDemo(CheckboxColumnDemo.class);
addDemo(EmptyGridLabelDemo.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -52,15 +55,33 @@ public LazyTestData() {
}

public Stream<Person> filter(int offset, int pageSize) {
return filter(offset, pageSize, null);
}

public Stream<Person> 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")) {
Expand All @@ -69,4 +90,12 @@ private static String generateCountry() {
return country;
}
}

@Getter
@Setter
public static class PersonFilter {
private String firstName;
private String lastName;
}

}
10 changes: 10 additions & 0 deletions src/test/resources/META-INF/resources/gridhelpers/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit a4922d7

Please sign in to comment.