I’m learning how to add filter fields in a grid by following
the tutorial
So far it’s great! I can add the filters in a row! But I have a column with dates, and wanted to implement a datefield in the filter.
I need help troubleshooting why this is happening.
So what I have is this:
[code]
BeanItemContainer beanList = new BeanItemContainer(Bean.class);
Grid resultsGrid = new Grid();
resultsGrid.setContainerDataSource(beanList);
HeaderRow filterRow = resultsGrid.appendHeaderRow();
for (Object pid: resultsGrid.getContainerDataSource().getContainerPropertyIds()){
HeaderCell cell = filterRow.getCell(pid);
if (pid.toString().contains("date")){
DateField filterDate = new DateField();
filterDate.setHeight("15px");
filterDate.setStyleName("filterField");
filterDate.setDateFormat("MM-dd-yyyy");
filterDate.addValueChangeListener(change ->{beanList.removeContainerFilters(pid);
if (change.getProperty() !=null)
System.out.println(filterDate.getValue().toString());
SimpleDateFormat filterFormat = new SimpleDateFormat("MM-dd-yyyy");
try{
System.out.println(filterFormat.format(filterDate.getValue()));
}catch(Exception e){
e.printStackTrace();
}
try{
beanList.addContainerFilter(new SimpleStringFilter(pid,filterFormat.format(filterDate.getValue()),true,false));
}catch(Exception e){
e.printStackTrace();
}
});
cell.setComponent(filterDate);
}else {
TextField filterField = new TextField();
filterField.setColumns(5);
filterField.setWidth("100%");
filterField.setHeight("15px");
filterField.setStyleName("filterField");
filterField.addTextChangeListener(change ->{beanList.removeContainerFilters(pid);
if (! change.getText().isEmpty())
beanList.addContainerFilter(new SimpleStringFilter(pid,change.getText(),true,false));
});
cell.setComponent(filterField);
}
[/code]Whenever I use the date filter, nothing works?
Beginning:
After Selecting Date:
I tested the output of
filterFormat.format(filterDate.getValue())
with a system.out.Println() and it says “11-02-2016”
However, just using a regular old textfield (not datefield) it works pretty good, I just need a leading zero on my day. But otherwise, it will filter! But I would really like to add a datefilter.
I am pretty bad at coding so any suggestions are welcome.