一、实现功能描述
搜索框可根据商品名称或商品编码 动态、即时搜索商品:
二、代码实现:
1.oninput:
function searchGoods(){
$("#dg").datagrid('load',{
"codeOrName":$("#s_codeOrName").val()});}
2.Controller:
/*** 根据条件分页查询没有库存的商品信息* @param codeOrName* @param page* @param rows* @return* @throws Exception*/@ResponseBody@RequestMapping("/listNoInventoryQuantity")@RequiresPermissions(value="期初库存")public Map<String,Object> listNoInventoryQuantity(@RequestParam(value="codeOrName",required=false)String codeOrName,@RequestParam(value="page",required=false)Integer page,@RequestParam(value="rows",required=false)Integer rows)throws Exception{
Map<String,Object> resultMap=new HashMap<>();List<Goods> goodsList=goodsService.listNoInventoryQuantityByCodeOrName(codeOrName, page, rows, Direction.ASC, "id");Long total=goodsService.getCountNoInventoryQuantityByCodeOrName(codeOrName);resultMap.put("rows", goodsList);resultMap.put("total", total);logService.save(new Log(Log.SEARCH_ACTION,"查询商品信息(无库存)"));return resultMap;}
3.ServiceImpl:
/*根据商品编码或商品名称条件分页查询没有库存(也就是库存为0的商品)的商品信息*/@Overridepublic List<Goods> listNoInventoryQuantityByCodeOrName(String codeOrName, Integer page, Integer pageSize,Direction direction, String... properties) {
Pageable pageable=new PageRequest(page-1,pageSize);Page<Goods> pageGoods=goodsRepository.findAll(new Specification<Goods>() {
@Overridepublic Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate=cb.conjunction();if(StringUtil.isNotEmpty(codeOrName)) {
predicate.getExpressions().add(cb.or(cb.like(root.get("code"), "%"+codeOrName+"%"),cb.like(root.get("name"), "%"+codeOrName+"%")));}predicate.getExpressions().add(cb.equal(root.get("inventoryQuantity"), 0));return predicate;}}, pageable);return pageGoods.getContent();}/*根据商品编码或商品名称条件分页查询没有库存的商品信息的总记录数*/@Overridepublic Long getCountNoInventoryQuantityByCodeOrName(String codeOrName) {
Long count=goodsRepository.count(new Specification<Goods>(){
@Overridepublic Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate predicate=cb.conjunction();if(StringUtil.isNotEmpty(codeOrName)) {
predicate.getExpressions().add(cb.or(cb.like(root.get("code"), "%"+codeOrName+"%"),cb.like(root.get("name"), "%"+codeOrName+"%")));}predicate.getExpressions().add(cb.equal(root.get("inventoryQuantity"), 0));return predicate;}});return count;}