SmartGWT - Filtering a ListGrid
SmartGWT is a rich framework that can be difficult to understand at first, espeically if you have no experience with Swing programming, but when you discover the UIBinder aspect you’ll be pleasantly surprised at what it can offer. I’m going to take a look at one of those offerings - filtering a ListGrid (or UIListGrid). We could get all the records from the list grid, and traverse them while applying a criteria but it’s a lot nicer to use the functionality inherit to this component framework. Let’s say we have a list of users and we display their forename and surname. Modern webapps offer live searching functionality which is a textbox with an onChange event used to trigger a search of a list using the contents of the text field (at the time when the event is triggered). SmartGWT offers the AdvacnedCriteria object that will accept a criteria value, attribute and operatorId. The criteria value is the content of the search field; the attribute is what we would like to apply our criteria value against; and the operatorId is how we’d like it applied. In the example below “miceal” is the criteria value which is applied against the “forename” attribute using the ICONTAINS operatorId. ICONTAINS means return matches where the criteria value is contained anywhere within the *attribute *value, case insensitive. Now we have our criteria, we can apply it against the ListGrid DataSource to display only the records that match that criterion.
```javaprivate void filterListOfNames(String AdvancedCriteria filterCriteria = new AdvancedCriteria(“forename”, OperatorId.ICONTAINS, findThisForename); lsgListOfNames.filterData(filterCriteria); }
Clearing the criteria to restore your original data set is quite simple:
```javalsgListOfNames.clearCriteria();
Please be aware that using a ListGrid without a list source is not fun, you’ll reap a lot of benefits building a DataSource first and then applying it to a ListGrid.