1.1 后台返回数据
PurchaseBillItemVo
private Long id; //编号
private String supplier; //供应商名称
private String buyer; //采购员名称
private String product; //产品名称
private String productType; //产品分类
private Date vdate; //交易时间
private BigDecimal num; //采购数量
private BigDecimal price; //价格
private BigDecimal amount; //小计 = 价格*数量
private Integer status;
private String groupField = ""; //分组字段
public PurchaseBillItemVo(){}
public PurchaseBillItemVo(PurchaseBillItem item,String groupBy){
this.id = item.getId();
this.supplier = item.getBill().getSupplier().getName();
this.buyer = item.getBill().getBuyer().getUsername();
this.product = item.getProduct().getName();
this.productType = item.getProduct().getTypes().getName();
this.vdate = item.getBill().getVdate();
this.num = item.getNum();
this.price = item.getPrice();
this.amount = item.getAmount();
this.status = item.getBill().getStatus();
//解决分组的问题,根据前台的传参使用不同的分组方案
//这里的分组名称比较奇怪,我们名称后面代码有作用,大家先不用管
if("o.bill.buyer.username".equals(groupBy)){
groupField = this.buyer;
}else if("MONTH(o.bill.vdate)".equals(groupBy)){
groupField = (DateUtils.toCalendar(vdate).get(Calendar.MONTH)+1) + "月份";
}else {
groupField = this.supplier;
}
PurchaseBillItemServiceImpl:
@Override
public List<PurchaseBillItemVo> findItems(PurchaseBillItemQuery itemQuery){
List<PurchaseBillItem> items = purchaseBillItemRepository.findByQuery(itemQuery);
List<PurchaseBillItemVo> vos = new ArrayList<>();
for (PurchaseBillItem item : items) {
PurchaseBillItemVo vo = new PurchaseBillItemVo(item,null);
vos.add(vo);
}
return vos;
}
PurchaseBillItemController
@RequestMapping("/findItems")
@ResponseBody
public Map<String,Object> findItems(PurchaseBillItemQuery itemQuery){
Map<String,Object> map = new HashMap();
map.put("rows",purchaseBillItemService.findItems(itemQuery));
return map;
}
purchaseBillItem.jsp
<head>
<title>Title</title>
<%@include file="/WEB-INF/views/head.jsp" %>
<!-- 引入datagrid-view的支持 -->
<script type="text/javascript" src="/easyui/plugins/datagrid-groupview.js"></script>
<script type="text/javascript" src="/js/model/purchaseBillItem.js"></script>
</head>
<table id="purchaseBillItemGrid"></table>
purchaseBillItem.js
ar purchaseBillItemGrid = $("#purchaseBillItemGrid");
…
purchaseBillItemGrid.datagrid({
nowrap:false,
fitColumns:true,
fit:true,
fixed:true,
fitColumns:true,
toolbar:'#tb',
url:'/purchaseBillItem/findItems',
columns:[[
{field:'id',title:'编号',width:100},
{field:'supplier',title:'供应商',width:100},
{field:'buyer',title:'采购员',width:100},
{field:'product',title:'产品',width:100},
{field:'productType',title:'产品类型',width:100},
{field:'vdate',title:'日期',width:100},
{field:'num',title:'数量',width:100},
{field:'price',title:'单价',width:100},
{field:'amount',title:'小计',width:100},
{field:'status',title:'状态',width:100,formatter: function (action) {
var data = {
0:"待审",
1:"已审",
"-1":"作废"
};
return data[action];
}}
]],
groupField:'groupField',
view: groupview,
groupFormatter:function(value, rows){
var totalNum = 0;
var totalAmount = 0;
for(var i=0;i<rows.length;i++){
totalNum += rows[i].num;
totalAmount += rows[i].amount;
}
return value + ' - ' + rows.length + ' 条数据' +" 共"+totalNum+"件商品" +"总金额:"+totalAmount+"";
}
});
1.2 带条件过滤查询
PurchaseBillItemQuery
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date beginDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate;
private Integer status;
private String groupBy = "o.bill.supplier.name";
@Override
public Specification createSpecification() {
Date tempDate = null;
if(endDate!=null){
tempDate = DateUtils.addDays(endDate,1);
}
//根据条件把数据返回即可
Specification<PurchaseBillItem> spec = Specifications.<PurchaseBillItem>and()
.eq(status!=null,"bill.status",status )//等于
.ge(beginDate!=null, "bill.vdate",beginDate) //大于等于
.lt(endDate!=null, "bill.vdate",tempDate) //小于等于
.build();
return spec;
}
PurchaseBillItemServiceImpl
生成vo的时候传入相应的条件
@Override
public List<PurchaseBillItemVo> findItems(PurchaseBillItemQuery itemQuery){
List<PurchaseBillItem> items = purchaseBillItemRepository.findByQuery(itemQuery);
List<PurchaseBillItemVo> vos = new ArrayList<>();
for (PurchaseBillItem item : items) {
PurchaseBillItemVo vo = new PurchaseBillItemVo(item,itemQuery.getGroupBy());
vos.add(vo);
}
return vos;
}
purchaseBillItem.jsp
<div id="tb" style="padding:5px;height:auto">
<!-- 这部门是查询的功能 -->
<div>
<div id="cc" class="easyui-calendar"></div>
<form id="searchForm" action="" method="post">
日期 : <input name="beginDate" class="easyui-datebox" style="height:32px" sharedCalendar="#cc">
- <input name="endDate" class="easyui-datebox" style="height:32px" sharedCalendar="#cc">
状态 :<select class="easyui-combobox" name="status"
data-options="panelHeight:'auto',width:100"
>
<option value="">--请选择--</option>
<option value="0">待审</option>
<option value="-1">作废</option>
<option value="1">已审</option>
</select>
<select class="easyui-combobox" name="groupBy"
data-options="panelHeight:'auto',width:100"
>
<option value="o.bill.supplier.name">供应商</option>
<option value="o.bill.buyer.username">采购员</option>
<option value="MONTH(o.bill.vdate)">月份</option>
</select>
<a href="#" data-method="search" class="easyui-linkbutton" iconCls="icon-search">查找</a>
</form>
</div>
</div>