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);
}
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);
}
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);
}
}
}
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);
}
}
}
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);
}
Aggregations