Hi I'm trying to write a grid filter in Vaadin framework. My aim is that a user has a combo box with dropdown list and can choose that he wants to see only entries from 3, 6, 9 weeks ago. I wrote it but I have a problem becasue it doesnt work as default filter. I mean if I open a page the grid is displayed without filter. I need first click on the button. Is it possible to filter grid by default, showing only 3 weeks entries? How can I manage it?
final ComboBox<Integer> timePeriodSelector = new ComboBox<>("Time Filter", Arrays.asList(3, 6, 9);
timePeriodSelector.setEmptySelectionAllowed(false);
timePeriodSelector.setTextInputAllowed(false);
timePeriodSelector.setValue(3);
timePeriodSelector.addValueChangeListener(this::onWeekChange);
...
private void onWeekChange(HasValue.ValueChangeEvent<Integer> event) {
switch (event.getValue()) {
case 3:
dateToCompare = LocalDate.now().minusDays(21);
break;
case 6:
dateToCompare = LocalDate.now().minusDays(42);
break;
case 9:
dateToCompare = LocalDate.now().minusDays(63);
break;
default:
dateToCompare = LocalDate.now().plusDays(21);
}
ListDataProvider<CcEvent> dataProvider = (ListDataProvider<CcEvent>) eventsOverview.getDataProvider();
dataProvider.setFilter(CcEvent::getReportingDate,
date -> compareDates(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), dateToCompare));
}
private Boolean compareDates(LocalDate dateFromTable, LocalDate dateFromFilter) {
return dateFromTable.isBefore(dateFromFilter);
}
that piece of code looks nice and is how the Grid filter is intented. However all you have to do is switch your
setValue
call with youraddValueChangeListener
call.Setting the listener before setting the value ensures that the
ValueChangedEvent
is triggered!