幸せのちから

          平凡的世界
          看似平常實崎嶇
          成如容易卻艱辛

          定制FilterCell

          定制FilterCell


          1. 引言

          列的filterCell屬性控制過濾器如何顯示,它和cell屬性非常相像并且也是實現(xiàn)Cell接口。馬上要定義的是默認的和droplist這兩個過濾器cells。 默認的是一個輸入框元素而droplist是一個下拉列表元素。當(dāng)然,如果你需要進行一些定制你可以插接自己的實現(xiàn)。

          最近,我被問到是否能夠?qū)崿F(xiàn)一個過濾器cell,顯示已經(jīng)通過別的過濾器過濾得到數(shù)據(jù)子集。答案當(dāng)然是肯定的,而且這是我將在這里示范的。通常定制的 cell可以很容易被創(chuàng)建,這個示例將印證這點。在這個示例里last name列里顯示的將是通過first name過濾后的子集。如果沒有通過 first name過濾那么所有值都將被顯示。

          1.1. 定制Droplist過濾器Cell示例

          通常你只需要為過濾器cell實現(xiàn)Cell接口。然而,因為我們要創(chuàng)建的過濾器cell是一個下拉列表,我們可以通過擴展 FilterDroplistCell來獲得它已經(jīng)提供的很多功能,F(xiàn)ilterDroplistCell是發(fā)行包已經(jīng)提供的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;
          }
          }

          如果你比較這個類和父類,你會發(fā)現(xiàn)它們只有微小的區(qū)別。

          首先需要注意的是我們需要找出first name是否已經(jīng)被過濾了。

          String firstNameFilter = model.getLimit().getFilterSet().getValue("firstName");

          然后我們需要判斷當(dāng)前bean的first name值是否和first name過濾器值相同。如果相同,將當(dāng)前的last name值 添加到droplist中。

          String firstName = BeanUtils.getProperty(bean, "firstName");
          if (StringUtils.isNotBlank(firstNameFilter) && !firstName.equals(firstNameFilter)) {
          continue;
          }

          如果last name將添加到droplist中,我們需要檢查droplist中是否已經(jīng)包含了這個值。如果沒有,我們就把它添加到droplist中。

          String lastName = BeanUtils.getProperty(bean, column.getProperty());
          if ((lastName != null) && !droplist.contains(lastName)) {
          droplist.add(lastName);
          }

          為了使用這個Cell你應(yīng)該在Preferences中聲明一個別名。 當(dāng)然,你可以省略這步而在JSP中提供這個Cell實現(xiàn)類的全路徑,但是使用Preferences更簡潔。

          column.filterCell.filteredDroplist=org.extremesite.cell.FilteredDroplistCell

          在ColumnTag通過設(shè)置filterCell屬性來使用FilteredDroplistCell。

          <ec:column property="lastName" filterCell="filteredDroplist"/>

          如果不清楚Preferences和ColumnTag定義語法請參考Preferences指南。

          posted on 2006-02-24 10:24 Lucky 閱讀(544) 評論(0)  編輯  收藏 所屬分類: extremeComponents

          <2006年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627281234
          567891011

          導(dǎo)航

          隨筆分類(125)

          文章分類(5)

          日本語

          搜索

          積分與排名

          最新隨筆

          最新評論

          主站蜘蛛池模板: 密云县| 肇源县| 平顶山市| 志丹县| 云浮市| 德惠市| 普兰县| 合江县| 北辰区| 浦城县| 乌兰县| 象山县| 丹江口市| 天镇县| 湘潭县| 枝江市| 吴堡县| 喀什市| 康马县| 陇西县| 平陆县| 英吉沙县| 桃园市| 年辖:市辖区| 资兴市| 鱼台县| 阜南县| 土默特右旗| 渑池县| 杭州市| 景宁| 明光市| 米泉市| 闽侯县| 台南市| 池州市| 重庆市| 盐津县| 滁州市| 闽侯县| 沈丘县|