列的filterCell属性控制过滤器如何显示,它和cell属性非常相像并且也是实现Cell接口。马上要定义的是默认的和droplist这两个过滤器cells。 默认的是一个输入框元素而droplist是一个下拉列表元素。当然,如果你需要进行一些定制你可以插接自己的实现。
最近,我被问到是否能够实现一个过滤器cell,显示已经通过别的过滤器过滤得到数据子集。答案当然是肯定的,而且这是我将在这里示范的。通常定制的 cell可以很容易被创建,这个示例将印证这点。在这个示例里last name列里显示的将是通过first name过滤后的子集。如果没有通过 first name过滤那么所有值都将被显示。
通常你只需要为过滤器cell实现Cell接口。然而,因为我们要创建的过滤器cell是一个下拉列表,我们可以通过扩展 FilterDroplistCell来获得它已经提供的很多功能,FilterDroplistCell是发行包已经提供的cells之一。
我们需要覆盖FilterDroplistCell的唯一方法是getFilterDropList()。这是整个类的全部代码:
public class FilteredDroplistCell extends FilterDroplistCell { private static Log logger = LogFactory.getLog(FilterDroplistCell.class); protected List getFilterDropList(TableModel model, Column column) { List droplist = new ArrayList(); String firstNameFilter = model.getLimit().getFilterSet().getValue("firstName"); Collection beans = model.getCollectionOfBeans(); for (Iterator iter = beans.iterator(); iter.hasNext();) { Object bean = iter.next(); try { String firstName = BeanUtils.getProperty(bean, "firstName"); if (StringUtils.isNotBlank(firstNameFilter) && !firstName.equals(firstNameFilter)) { continue; } String lastName = BeanUtils.getProperty(bean, column.getProperty()); if ((lastName != null) && !droplist.contains(lastName)) { droplist.add(lastName); } } catch (Exception e) { logger.debug("Problems getting the droplist.", e); } } Collections.sort(droplist); return droplist; } }
如果你比较这个类和父类,你会发现它们只有微小的区别。
首先需要注意的是我们需要找出first name是否已经被过滤了。
String firstNameFilter = model.getLimit().getFilterSet().getValue("firstName");
然后我们需要判断当前bean的first name值是否和first name过滤器值相同。如果相同,将当前的last name值 添加到droplist中。
String firstName = BeanUtils.getProperty(bean, "firstName"); if (StringUtils.isNotBlank(firstNameFilter) && !firstName.equals(firstNameFilter)) { continue; }
如果last name将添加到droplist中,我们需要检查droplist中是否已经包含了这个值。如果没有,我们就把它添加到droplist中。
String lastName = BeanUtils.getProperty(bean, column.getProperty()); if ((lastName != null) && !droplist.contains(lastName)) { droplist.add(lastName); }
为了使用这个Cell你应该在Preferences中声明一个别名。 当然,你可以省略这步而在JSP中提供这个Cell实现类的全路径,但是使用Preferences更简洁。
column.filterCell.filteredDroplist=org.extremesite.cell.FilteredDroplistCell
在ColumnTag通过设置filterCell属性来使用FilteredDroplistCell。
<ec:column property="lastName" filterCell="filteredDroplist"/>
如果不清楚Preferences和ColumnTag定义语法请参考Preferences指南。