In my previous post, I have shown how to use built-in filtering in RichFaces. The basic filtering uses startsWith() method to filter. This is fine but you might have a situation where you need to write your own custom filtering method.
To keep things simple, let’s apply custom filtering to Name column only. For Location column we will keep the standard filtering.
Name
Location
Image
Most of the stuff happening inside the first rich:column tag starting at line #4. First I define the actual custom filtering method via filterMethod attribute. I will come back to is shortly. The rest is just an input field with a4j:support with onkeyup event. One the filtering is done, we return the focus to input field with focus attribute.
The changes to managed bean are minor. We have to add the filtering method and the filterValue property (this property holds the value we enter on the page).
...
private String filterValue = "";
// getter and setter method for filterValue
public boolean filterWonders(Object current) {
Wonder currentWonder = (Wonder)current;
if (filterValue.length()==0) {
return true;
}
if (currentWonder.getName().startsWith(filterValue)) {
return true;
}
else {
return false;
}
}
...
The method returns boolean value. The value entered from the page is checked against each value in the list. Value of true means include the current object in the filtering result. The filterWonders is not much different from using the built-in starsWith() method, however, you are free to add more interesting filtering in this method as you see fit. One thing different is that the method requires exact case. In other words, entering ‘A’ or ‘a’ is not the same. This is just to show you to get setup custom filtering.
An alternative approach is to use filterExpression instead of filterMethod. It basically works the same way. It has to evaluate to either true or false. true means the value is included in the filtering result.
...
Leave a reply to max Cancel reply