实现报表列表过滤原创
252次浏览
未经作者许可,禁止转载编辑于2024年09月19日 10:29:14
在我们开发的过程中经常会遇到这样的需求,要在报表列表实现和单据列表一样的过滤。
要实现这个需求我们大致分为以下步骤
1、取数插件返回DataSet前,需要调用编写的工具类将结果集和过滤条件处理(工具类放在文章的最后(仅供参考),有需要的自取)。 dataset = FilterUtils.HeaderFilter(dataset, filterInfo); 2、报表的单据插件重写setSortAndFilter方法,下边是报表所有的字段都可以过滤,也可以只加需要过滤的字段 @Override public void setSortAndFilter(List<SortAndFilterEvent> list) { super.setSortAndFilter(list); for(SortAndFilterEvent event : list){ event.setFilter(true); event.setSort(true); } } /** *实现报表列表过滤的方法 */ public static DataSet headerFilter(DataSet resultDataSet, FilterInfo filterInfo) { List<QFilter> headFilters = filterInfo.getHeadFilters(); List<String> cpList = Arrays.asList("is not null,is null".split(",")); List<String> dataTypeList = Arrays.asList("BigDecimal,Long,Timestamp".split(",")); //表头添加过滤 if (headFilters != null && headFilters.size() > 0) { QFilter qFilter = new QFilter("1", QCP.equals, 1); for (int i = 0; i < headFilters.size(); i++) { QFilter filter = headFilters.get(i); String property = filter.getProperty(); String cp = filter.getCP(); Object value = filter.getValue(); List<String> list = new ArrayList<>(Arrays.asList(resultDataSet.getRowMeta().getFieldNames())); for (String field : list) { DataType dataType = resultDataSet.getRowMeta().getField(field).getDataType(); if (property.equals(field)) { if (cpList.contains(cp) || (value != null && dataTypeList.contains(dataType.getName()))) { //校验操作符 if ("is not null".equals(cp)) { qFilter.and(new QFilter(property, QCP.is_notnull, null)) .and(new QFilter(property, QCP.not_equals, "")); } else if (StringUtils.equals("is null", cp)) { QFilter orQFilter = new QFilter(property, QCP.is_null, null) .or(new QFilter(property, QCP.equals, "")); qFilter.and(orQFilter); } //校验值 if (dataType.getName().equals("BigDecimal")) { String val = value.toString(); BigDecimal bigDecimal = new BigDecimal(val); qFilter.and(new QFilter(property, cp, bigDecimal)); } if (dataType.getName().equals("Long")) { String val = value.toString(); Long bigDecimal = Long.valueOf(val); qFilter.and(new QFilter(property, cp, bigDecimal)); } if (dataType.getName().equals("Timestamp")) { String val = value.toString(); resultDataSet = MyReportGeneralOpHelper.changFiledDataType(resultDataSet, property, String.class); if (cp.equals("=")) { QFilter dateFilter = new QFilter(property, QCP.like, "%" + val + "%"); resultDataSet = resultDataSet.filter(dateFilter.toString()); }else { String startStr = filter.toString().split(">=")[1].split("AND")[0]; String endStr = filter.toString().split("<")[1]; Date startDate = getDate(startStr); Date endDate = getDate(endStr); QFilter dateFilter = new QFilter(property,QCP.large_equals,startDate) .and(new QFilter(property,QCP.less_than,endDate)); resultDataSet = resultDataSet.filter(dateFilter.toString()); } resultDataSet = MyReportGeneralOpHelper.changFiledDataType(resultDataSet, property, Timestamp.class); } } else { qFilter.and(filter); } } } } resultDataSet = resultDataSet.filter(qFilter.toString()); } return resultDataSet; } /** * 日期时间字符串转为Date * * @param date * @return */ private static Date getDate(String date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { if (date.contains("'")){ date = date.replaceAll("'","").trim(); } return simpleDateFormat.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; }
赞 0
0人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读