关注JEECG发展历程 关注最新动态和版本, 记录JEECG成长点滴 更新日志 - 技术支持 - 招聘英才

JEECG最新版本下载 JEECG智能开发平台 - 显著提高开发效率 常见问题 - 入门视频 - 参与开源团队

商务QQ: 69893005、3102411850 商务热线(5*8小时): 010-64808099 官方邮箱: jeecgos@163.com

查看: 8661|回复: 0

[代码生成器] datagrid条件查询问题

[复制链接]
发表于 2018-5-19 20:54:40 | 显示全部楼层 |阅读模式
自定义的代码。非生成器。
查询正常。但当多页时,选择最后页。然后录入条件,点击查询,会多次加载datagrid方法。求解


JSP代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  2. <%@include file="/context/mytags.jsp" %>
  3. <t:base type="jquery,easyui,tools,DatePicker"></t:base>
  4. <div class="easyui-layout" fit="true">
  5.     <div region="center" style="padding:0px;border:0px">

  6.         <t:datagrid name="techownReportList" checkbox="false" pagination="true" fitColumns="false" title="账号明细汇总"
  7.                     actionUrl="" idField="id" fit="true" queryMode="group">
  8.             <t:dgCol title="主键" field="id" hidden="true" queryMode="single" width="120"></t:dgCol>
  9.             <t:dgCol title="API账号" field="apiname" queryMode="group" width="120"></t:dgCol>
  10.             <t:dgCol title="客户经理" field="sale" queryMode="group" width="120"></t:dgCol>
  11.             <t:dgCol title="账号名称" field="accountname" query="true" queryMode="single" width="120"></t:dgCol>
  12.             <t:dgCol title="客户简称" field="customershort" query="true" queryMode="single" width="120"></t:dgCol>
  13.             <t:dgCol title="单价" field="price" queryMode="group" width="120"></t:dgCol>
  14.             <t:dgCol title="账号行业分类" field="industryclassification" queryMode="group" width="120"></t:dgCol>
  15.             <t:dgCol title="账号类型" field="accounttype" queryMode="group" width="120"></t:dgCol>
  16.             <t:dgCol title="计费方式" field="billingway" queryMode="group" width="120"></t:dgCol>
  17.             <t:dgCol title="付费方式" field="paymentway" queryMode="group" width="120"></t:dgCol>
  18.             <t:dgCol title="当月合计" field="summonth" queryMode="group" width="120"></t:dgCol>
  19.             <t:dgCol title="日期" field="date" formatter="yyyy-MM-dd" hidden="true" query="true" queryMode="group"
  20.                      width="120"></t:dgCol>
  21.             <t:dgToolBar title="导出" icon="icon-putout" funname="ExportXls"></t:dgToolBar>
  22.         </t:datagrid>
  23.     </div>
  24. </div>
  25. <script type="text/javascript">
  26.     $(document).ready(function () {
  27.     });
  28.     $(function () {
  29.         var date_begin = $("input[name='date_begin']").val();
  30.         var date_end = $("input[name='date_end']").val();
  31.         fetchData(date_begin, date_end);
  32.     });

  33.     function fetchData(date_begin, date_end) {
  34.         var accountname = $("input[name='accountname']").val();
  35.         var customershort = $("input[name='customershort']").val();
  36.         if (date_begin == undefined && date_end == undefined) {
  37.             date_begin = new Date();
  38.             date_end = new Date();
  39.         }
  40.         if (date_end == undefined && date_begin != undefined) {
  41.             date_end = new Date();
  42.         }
  43.         var s = "";
  44.         s = "[[";
  45.         s = s + "{field:'id',title:'主键',width:120,hidden:'true',frozenColumn:'true'},{field:'apiname',title:'API账号',width:120,frozenColumn:'true'},{field:'sale',title:'客户经理',width:120,frozenColumn:'true'},{field:'accountname',title:'账号名称',width:120,frozenColumn:'true'},{field:'customershort',title:'客户简称',width:120,frozenColumn:'true'},{field:'price',title:'单价',width:120,frozenColumn:'true'},{field:'industryclassification',title:'账号行业分类',width:120,frozenColumn:'true'},{field:'accounttype',title:'账号类型',width:120,frozenColumn:'true'},{field:'billingway',title:'计费方式',width:120,frozenColumn:'true'},{field:'paymentway',title:'付费方式',width:120,frozenColumn:'true'},{field:'summonth',title:'当月合计',width:120,frozenColumn:'true'},";

  46.         if (dateIsBig(date_begin, date_end)) {
  47.             var dates = getDates(date_begin, date_end);
  48.             for (var i = 0; i < dates.length; i++) {
  49.                 s = s + "{field:'" + dates[i] + "',title:'" + dates[i] + "',width:120,frozenColumn:'true'},"
  50.             }
  51.         }
  52.         s = s.substring(0, s.length - 1);
  53.         s = s + "]]";
  54.         options = {};
  55.         options.url = 'techownAccountDetailsController.do?datagrid&field=id,apiname,apiname_begin,apiname_end,sale,sale_begin,sale_end,accountname,customershort,price,price_begin,price_end,industryclassification,industryclassification_begin,industryclassification_end,accounttype,accounttype_begin,accounttype_end,billingway,billingway_begin,billingway_end,paymentway,paymentway_begin,paymentway_end,summonth,summonth_begin,summonth_end,date,date_begin,date_end';
  56.         options.queryParams = {
  57.             accountname: accountname,
  58.             customershort: customershort,
  59.             date_begin: date_begin,
  60.             date_end: date_end,
  61.         };
  62.         options.columns = eval(s);

  63.         $('#techownReportList').datagrid(options);
  64.     }

  65.     function dateIsBig(date_begin, date_end) {
  66.         if (date_begin.undefined && date_end.undefined) {
  67.             return false
  68.         } else {
  69.             var start = new Date(date_begin.replace("-", "/").replace("-", "/"));
  70.             var end = new Date(date_end.replace("-", "/").replace("-", "/"));
  71.             if (start <= end) {
  72.                 return true;
  73.             } else {
  74.                 return false;
  75.             }
  76.         }
  77.     }

  78.     function getDates(date_begin, date_end) {
  79.         var ab = date_begin.split("-");
  80.         var ae = date_end.split("-");
  81.         var db = new Date();
  82.         db.setFullYear(ab[0], ab[1] - 1, ab[2]);
  83.         var de = new Date();
  84.         de.setFullYear(ae[0], ae[1] - 1, ae[2]);
  85.         var unixDb = db.getTime();
  86.         var unixDe = de.getTime();
  87.         var dates = [];
  88.         console.log(date_begin);
  89.         for (var k = unixDb; k <= unixDe;) {
  90.             dates.push((new Date(parseInt(k))).format());
  91.             k = k + 24 * 60 * 60 * 1000;
  92.         }
  93.         return dates;
  94.     }

  95.     Date.prototype.format = function () {
  96.         var s = '';
  97.         s += this.getFullYear() + '-';          // 获取年份。
  98.         s += (this.getMonth() + 1) >= 10 ? (this.getMonth() + 1) : "0" + (this.getMonth() + 1) + "-";         // 获取月份。
  99.         s += this.getDate() >= 10 ? this.getDate() : "0" + this.getDate();                 // 获取日。
  100.         return (s);                          // 返回日期。
  101.     };

  102.     //导出
  103.     function ExportXls() {
  104.         JeecgExcelExport("techownAccountDetailsController.do?exportXls", "techownReportList");
  105.     }

  106.     function techownReportListsearch() {
  107.         var date_begin = $("input[name='date_begin']").val();
  108.         var date_end = $("input[name='date_end']").val();
  109.         fetchData(date_begin, date_end);
  110.     }
  111. </script>
复制代码
controller代码
  1. package com.jeecg.details.controller;

  2. import com.jeecg.details.dao.TechownAccountDetailsDao;
  3. import com.jeecg.details.entity.TechownAccountDetailsEntity;
  4. import com.jeecg.utils.ExportXlsUtil;
  5. import io.swagger.annotations.Api;
  6. import org.apache.log4j.Logger;
  7. import org.jeecgframework.core.common.controller.BaseController;
  8. import org.jeecgframework.core.common.model.json.DataGrid;
  9. import org.jeecgframework.tag.core.easyui.TagUtil;
  10. import org.jeecgframework.web.system.service.SystemService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.servlet.ModelAndView;

  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.validation.Validator;
  19. import java.text.SimpleDateFormat;
  20. import java.util.*;

  21. /**
  22. * @author onlineGenerator
  23. * @version V1.0
  24. * @Title: Controller
  25. * @Description: 账号明细汇总
  26. * @date 2018-03-26 10:29:03
  27. */
  28. @Api(value = "TechownAccountDetails", description = "账号明细汇总", tags = "techownAccountDetailsController")
  29. @Controller
  30. @RequestMapping("/techownAccountDetailsController")
  31. public class TechownAccountDetailsController extends BaseController {
  32.     /**
  33.      * Logger for this class
  34.      */
  35.     private static final Logger logger = Logger.getLogger(TechownAccountDetailsController.class);

  36.     @Autowired
  37.     private SystemService systemService;
  38.     @Autowired
  39.     private Validator validator;
  40.     @Autowired
  41.     private TechownAccountDetailsDao techownAccountDetailsDao;

  42.     /**
  43.      * 账号明细汇总列表 页面跳转
  44.      *
  45.      * @return
  46.      */
  47.     @RequestMapping(params = "list")
  48.     public ModelAndView list(HttpServletRequest request) {
  49.         return new ModelAndView("com/jeecg/details/techownAccountDetailsList");
  50.     }

  51.     /**
  52.      * easyui AJAX请求数据
  53.      *
  54.      * @param request
  55.      * @param response
  56.      * @param dataGrid
  57.      * @param
  58.      */

  59.     @RequestMapping(params = "datagrid")
  60.     public void datagrid(TechownAccountDetailsEntity techownAccountDetailsEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  61.         //查询所有账号信息
  62.         Integer pageNumber = dataGrid.getPage();
  63.         Integer pageSize = dataGrid.getRows();
  64.         Integer beginLimit = (pageNumber - 1) * pageSize;
  65.         Integer endLimit = pageSize;
  66.         System.out.println("打印几次------" + pageNumber + "===" + pageSize);
  67.         String accountname = techownAccountDetailsEntity.getAccountname() == "" ? null : "%" + techownAccountDetailsEntity.getAccountname() + "%";
  68.         String customersort = techownAccountDetailsEntity.getCustomershort() == "" ? null : "%" + techownAccountDetailsEntity.getCustomershort() + "%";
  69.         List<Map<String, Object>> list = techownAccountDetailsDao.getAccountDetails(accountname, customersort, beginLimit, endLimit);
  70.         List<String> dates = getMonthBetweenDate(techownAccountDetailsEntity.getDate_begin(), techownAccountDetailsEntity.getDate_end());
  71.         Map<String, Map<String, Object>> extMap = new HashMap<String, Map<String, Object>>();
  72.         for (int i = 0; i < list.size(); i++) {
  73.             Map<String, Object> map = list.get(i);
  74.             if (dates != null) {
  75.                 String apinameEach = String.valueOf(list.get(i).get("apiname"));
  76.                 for (int j = 0; j < dates.size(); j++) {
  77.                     Integer each = techownAccountDetailsDao.getEachApiSendByDate(apinameEach, dates.get(j));
  78.                     map.put(dates.get(j), each == null ? 0 : each);
  79.                 }
  80.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  81.                 String begin = sdf.format(techownAccountDetailsEntity.getDate_begin() == null ? new Date() : techownAccountDetailsEntity.getDate_begin());
  82.                 String end = sdf.format(techownAccountDetailsEntity.getDate_end() == null ? new Date() : techownAccountDetailsEntity.getDate_end());
  83.                 Integer summonth = techownAccountDetailsDao.getSumApiSendByDate(apinameEach, begin, end);
  84.                 map.put("summonth", summonth == null ? 0 : summonth);
  85.             }
  86.             extMap.put(list.get(i).get("ID").toString(), map);
  87.         }
  88.         dataGrid.setTotal(techownAccountDetailsDao.getAccountDetailsCount(accountname, customersort));
  89.         TagUtil.datagrid2(response, dataGrid, extMap);
  90.     }

  91.     public static List<String> getMonthBetweenDate(Date beginDate, Date endDate) {
  92.         List<String> lDate = new ArrayList<String>();
  93.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  94.         if (beginDate != null && endDate != null) {
  95.             if (beginDate.getTime() == endDate.getTime()) {
  96.                 return null;
  97.             }
  98.             lDate.add(sdf.format(beginDate));//把开始时间加入集合
  99.             Calendar cal = Calendar.getInstance();
  100.             //使用给定的 Date 设置此 Calendar 的时间
  101.             cal.setTime(beginDate);
  102.             boolean bContinue = true;
  103.             while (bContinue) {
  104.                 //根据日历的规则,为给定的日历字段添加或减去指定的时间量
  105.                 cal.add(Calendar.DAY_OF_MONTH, 1);
  106.                 // 测试此日期是否在指定日期之后
  107.                 if (endDate.after(cal.getTime())) {
  108.                     Date m = cal.getTime();
  109.                     lDate.add(sdf.format(m));
  110.                 } else {
  111.                     break;
  112.                 }
  113.             }
  114.             lDate.add(sdf.format(endDate));//把结束时间加入集合
  115.         }
  116.         return lDate;
  117.     }


  118.     /**
  119.      * 导出excel
  120.      *
  121.      * @param request
  122.      * @param response
  123.      */
  124.     @RequestMapping(params = "exportXls", method = RequestMethod.GET)
  125.     public void exportXls(TechownAccountDetailsEntity techownAccountDetailsEntity, HttpServletRequest request, HttpServletResponse response) throws Exception {
  126.         List<Map<String, Object>> list = techownAccountDetailsDao.getAccountDetailsAll(techownAccountDetailsEntity.getAccountname(), techownAccountDetailsEntity.getCustomershort());
  127.         List<String> dates = getMonthBetweenDate(techownAccountDetailsEntity.getDate_begin(), techownAccountDetailsEntity.getDate_end());
  128.         List<Map<String, Object>> extMap = new ArrayList<>();
  129.         Map<String, Object> map = new HashMap<String, Object>();
  130.         map.put("apiname", "API账号");
  131.         map.put("sale", "客户经理");
  132.         map.put("accountname", "账号名称");
  133.         map.put("customershort", "客户简称");
  134.         map.put("price", "单价");
  135.         map.put("industryclassification", "账号行业分类");
  136.         map.put("accounttype", "账号类型");
  137.         map.put("billingway", "计费方式");
  138.         map.put("paymentway", "付费方式");
  139.         map.put("summonth", "当月合计");
  140.         for (int m = 0; m < dates.size(); m++) {
  141.             map.put(dates.get(m), dates.get(m));
  142.         }
  143.         extMap.add(map);
  144.         for (int i = 0; i < list.size(); i++) {
  145.             map = list.get(i);
  146.             if (dates != null) {
  147.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  148.                 Integer summonth = techownAccountDetailsDao.getSumApiSendByDate(list.get(i).get("apiname").toString(), sdf.format(techownAccountDetailsEntity.getDate_begin()), sdf.format(techownAccountDetailsEntity.getDate_end()));
  149.                 map.put("summonth", summonth == null ? 0 : summonth);
  150.                 for (int j = 0; j < dates.size(); j++) {
  151.                     Integer each = techownAccountDetailsDao.getEachApiSendByDate(list.get(i).get("apiname").toString(), dates.get(j));
  152.                     map.put(dates.get(j), each == null ? 0 : each);
  153.                 }
  154.             }
  155.             extMap.add(map);
  156.         }
  157.         String fileName = new String(("账号明细汇总" + ".xls").getBytes(), "iso-8859-1");
  158.         ExportXlsUtil.setResponse(response, fileName, extMap);
  159.     }
  160. }
复制代码
视频地址
链接:https://pan.baidu.com/s/1gIE4iLz0UVn0CEu8wwuScQ 密码:r296





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表