|  | 
 
| 做报表的时候发现datagrid方法多次触发,且pageNumber自动改变。触发情景:刚进入页面时多次加载;点击查询时多次加载 
   
 
 这个是我的jsp报表页面。需要动态生成列
 以下是我的controller类复制代码<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<div class="easyui-layout" fit="true">
    <div region="center" style="padding:0px;border:0px">
        <t:datagrid name="techownReportList" checkbox="true" pagination="true" fitColumns="true" title="账号明细查询"
                    actionUrl="techownAccountDetailsController.do?datagrid" idField="id" fit="true" queryMode="group">
            <t:dgCol title="主键" field="id" hidden="true" queryMode="single" width="120"></t:dgCol>
            <t:dgCol title="API账号" field="apiname" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="客户经理" field="sale" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="账号名称" field="accountname" query="true" queryMode="single" width="120"></t:dgCol>
            <t:dgCol title="客户简称" field="customershort" query="true" queryMode="single" width="120"></t:dgCol>
            <t:dgCol title="单价" field="price" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="账号行业分类" field="industryclassification" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="账号类型" field="accounttype" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="计费方式" field="billingway" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="付费方式" field="paymentway" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="当月合计" field="summonth" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="日期" field="date" formatter="yyyy-MM-dd" hidden="true" query="true" queryMode="group"
                     width="120"></t:dgCol>
            <t:dgToolBar title="导出" icon="icon-putout" funname="ExportXls"></t:dgToolBar>
        </t:datagrid>
    </div>
</div>
<script type="text/javascript">
    var options = {};
    $(function () {
        var accountname = $("input[name='accountname']").val();
        var customershort = $("input[name='customershort']").val();
        var date_begin = $("input[name='date_begin']").val();
        var date_end = $("input[name='date_end']").val();
        //初始化
        $("#techownReportList").datagrid({
            type: 'POST',
            nowrap: false,
            striped: true,
            fit: true,
            width: 1024,
            height: 500,
            url: '',
            pageSize: 10,
            remoteSort: false,
            pagination: true,
            rownumbers: true,
            singleSelect: true,
            fitColumns: false,
            queryParams: {
                accountname: accountname,
                customershort: customershort,
                date_begin: date_begin,
                date_end: date_end,
            }
        });
        fetchData(date_begin, date_end);
    });
    function fetchData(date_begin, date_end) {
        var accountname = $("input[name='accountname']").val();
        var customershort = $("input[name='customershort']").val();
        var date_begin = $("input[name='date_begin']").val();
        var date_end = $("input[name='date_end']").val();
        if (date_begin == undefined && date_end == undefined) {
        }
        if (date_end == undefined && date_begin != undefined) {
        }
        var s = "";
        s = "[[";
        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'},";
        if (dateIsBig(date_begin, date_end)) {
            var dates = getDates(date_begin, date_end);
            for (var i = 0; i < dates.length; i++) {
                s = s + "{field:'" + dates[i] + "',title:'" + dates[i] + "',width:120,frozenColumn:'true'},"
            }
        }
        s = s.substring(0, s.length - 1);
        s = s + "]]";
        options = {};
        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';
        options.queryParams = {
            accountname: accountname,
            customershort: customershort,
            date_begin: date_begin,
            date_end: date_end,
        };
        options.columns = eval(s);
        $('#techownReportList').datagrid(options);
        $('#techownReportList').datagrid('reload');
    }
    function dateIsBig(date_begin, date_end) {
        if (date_begin.undefined && date_end.undefined) {
            return false
        } else {
            var start = new Date(date_begin.replace("-", "/").replace("-", "/"));
            var end = new Date(date_end.replace("-", "/").replace("-", "/"));
            if (start <= end) {
                return true;
            } else {
                return false;
            }
        }
    }
    function getDates(date_begin, date_end) {
        var ab = date_begin.split("-");
        var ae = date_end.split("-");
        var db = new Date();
        db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
        var de = new Date();
        de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
        var unixDb = db.getTime();
        var unixDe = de.getTime();
        var dates = [];
        for (var k = unixDb; k <= unixDe;) {
            dates.push((new Date(parseInt(k))).format());
            k = k + 24 * 60 * 60 * 1000;
        }
        return dates;
    }
    Date.prototype.format = function () {
        var s = '';
        s += this.getFullYear() + '-';          // 获取年份。
        s += (this.getMonth() + 1) >= 10 ? (this.getMonth() + 1) : "0" + (this.getMonth() + 1) + "-";         // 获取月份。
        s += this.getDate() >= 10 ? this.getDate() : "0" + this.getDate();                 // 获取日。
        return (s);                          // 返回日期。
    };
    //导出
    function ExportXls() {
        JeecgExcelExport("techownAccountDetailsController.do?exportXls", "techownReportList");
    }
    function techownReportListsearch() {
        var date_begin = $("input[name='date_begin']").val();
        var date_end = $("input[name='date_end']").val();
        fetchData(date_begin, date_end);
        try {
            if (!$("#techownReportListForm").Validform({tiptype: 3}).check()) {
                return false;
            }
        } catch (e) {
        }
        if (true) {
            var queryParams = $('#techownReportList').datagrid('options').queryParams;
            $('#techownReportListtb').find('*').each(function () {
                queryParams[$(this).attr('name')] = $(this).val();
            });
            $('#techownReportList').datagrid({
                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,',
                pageNumber: 1,
            });
        }
    }
</script>
复制代码package com.jeecg.techown.controller;
import com.jeecg.techown.dao.TechownAccountDetailsDao;
import com.jeecg.techown.entity.TechownAccountDetailsEntity;
import com.jeecg.techown.util.ExportXlsUtil;
import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Validator;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author onlineGenerator
 * @version V1.0
 * @Title: Controller
 * @Description: 账号明细汇总
 * @date 2018-03-26 10:29:03
 */
@Api(value = "TechownAccountDetails", description = "账号明细汇总", tags = "techownAccountDetailsController")
@Controller
@RequestMapping("/techownAccountDetailsController")
public class TechownAccountDetailsController extends BaseController {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(TechownAccountDetailsController.class);
    @Autowired
    private SystemService systemService;
    @Autowired
    private Validator validator;
    @Autowired
    private TechownAccountDetailsDao techownAccountDetailsDao;
    /**
     * 账号明细汇总列表 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "list")
    public ModelAndView list(HttpServletRequest request) {
        return new ModelAndView("com/jeecg/techown/techownAccountDetailsList");
    }
    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     * @param
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(TechownAccountDetailsEntity techownAccountDetailsEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        //查询所有账号信息
        Integer pageNumber = dataGrid.getPage();
        Integer pageSize = dataGrid.getRows();
        Integer beginLimit = (pageNumber - 1) * pageSize;
        Integer endLimit = pageSize;
        System.out.println("打印几次------" + pageNumber + "===" + pageSize);
        List<Map<String, Object>> list = techownAccountDetailsDao.getAccountDetails("%" + techownAccountDetailsEntity.getAccountname() + "%", "%" + techownAccountDetailsEntity.getCustomershort() + "%", beginLimit, endLimit);
        List<String> dates = getMonthBetweenDate(techownAccountDetailsEntity.getDate_begin(), techownAccountDetailsEntity.getDate_end());
        Map<String, Map<String, Object>> extMap = new HashMap<String, Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            if (dates != null) {
                for (int j = 0; j < dates.size(); j++) {
                    Integer each = techownAccountDetailsDao.getEachApiSendByDate(list.get(i).get("apiname").toString(), dates.get(j));
                    map.put(dates.get(j), each == null ? 0 : each);
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String begin = techownAccountDetailsEntity.getDate_begin() == null ? "" : sdf.format(techownAccountDetailsEntity.getDate_begin());
                String end = techownAccountDetailsEntity.getDate_end() == null ? "" : sdf.format(techownAccountDetailsEntity.getDate_end());
                Integer summonth = techownAccountDetailsDao.getSumApiSendByDate(list.get(i).get("apiname").toString(), begin, end);
                map.put("summonth", summonth == null ? 0 : summonth);
            }
            extMap.put(list.get(i).get("ID").toString(), map);
        }
        dataGrid.setTotal(techownAccountDetailsDao.getAccountDetailsCount("%" + techownAccountDetailsEntity.getAccountname() + "%", "%" + techownAccountDetailsEntity.getCustomershort() + "%"));
        TagUtil.datagrid(response, dataGrid, extMap);
    }
    public static List<String> getMonthBetweenDate(Date beginDate, Date endDate) {
        List<String> lDate = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (beginDate != null && endDate != null) {
            if (beginDate.getTime() == endDate.getTime()) {
                return null;
            }
            lDate.add(sdf.format(beginDate));//把开始时间加入集合
            Calendar cal = Calendar.getInstance();
            //使用给定的 Date 设置此 Calendar 的时间
            cal.setTime(beginDate);
            boolean bContinue = true;
            while (bContinue) {
                //根据日历的规则,为给定的日历字段添加或减去指定的时间量
                cal.add(Calendar.DAY_OF_MONTH, 1);
                // 测试此日期是否在指定日期之后
                if (endDate.after(cal.getTime())) {
                    Date m = cal.getTime();
                    lDate.add(sdf.format(m));
                } else {
                    break;
                }
            }
            lDate.add(sdf.format(endDate));//把结束时间加入集合
        }
        return lDate;
    }
    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "exportXls", method = RequestMethod.GET)
    public void exportXls(TechownAccountDetailsEntity techownAccountDetailsEntity, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = techownAccountDetailsDao.getAccountDetailsAll(techownAccountDetailsEntity.getAccountname(), techownAccountDetailsEntity.getCustomershort());
        List<String> dates = getMonthBetweenDate(techownAccountDetailsEntity.getDate_begin(), techownAccountDetailsEntity.getDate_end());
        List<Map<String, Object>> extMap = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("apiname", "API账号");
        map.put("sale", "客户经理");
        map.put("accountname", "账号名称");
        map.put("customershort", "客户简称");
        map.put("price", "单价");
        map.put("industryclassification", "账号行业分类");
        map.put("accounttype", "账号类型");
        map.put("billingway", "计费方式");
        map.put("paymentway", "付费方式");
        map.put("summonth", "当月合计");
        for (int m = 0; m < dates.size(); m++) {
            map.put(dates.get(m), dates.get(m));
        }
        extMap.add(map);
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            if (dates != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Integer summonth = techownAccountDetailsDao.getSumApiSendByDate(list.get(i).get("apiname").toString(), sdf.format(techownAccountDetailsEntity.getDate_begin()), sdf.format(techownAccountDetailsEntity.getDate_end()));
                map.put("summonth", summonth == null ? 0 : summonth);
                for (int j = 0; j < dates.size(); j++) {
                    Integer each = techownAccountDetailsDao.getEachApiSendByDate(list.get(i).get("apiname").toString(), dates.get(j));
                    map.put(dates.get(j), each == null ? 0 : each);
                }
            }
            extMap.add(map);
        }
        String fileName = new String(("账号明细汇总" + ".xls").getBytes(), "iso-8859-1");
        ExportXlsUtil.setResponse(response, fileName, extMap);
    }
}
 | 
 |