js a标签 href与button onclick 不同,在方法调用上存在差异
使用herf与button的onclick导出Excel,在两个不同的form中,使用同一个form的参数
--HTML--
<div title="数据展示表格" id="tab_records" style='width: 99%;'>
<div id="toolbar">
<form id="searchform" class="layui-form" style="font-size: 12px;">
<div>
组织机构 <input id="sm_like_orgname" name="sm_like_orgname" />
</div>
<div>
关键字 <input id="sm_like_keyword" name="sm_like_keyword" />
</div>
日期
<div class="layui-input-inline">
<input type="text" id="sm_to1_createtime" name="sm_to1_createtime" class="date"
/>
</div>
-
<div class="layui-input-inline">
<input type="text" id="sm_to2_createtime" name="sm_to2_createtime" class="date" />
</div>
<div class="queryBtnClass">
<a href="javascript:getData();"> <i
class="fa fa-search #575D66 "></i> 查询
</a>
<a href="javascript:exports()"> <i class="fa fa-plus #575D66 "></i>
导出Excel
</a>
</div>
<input type="hidden" id="sm_orderby" name="sm_orderby" value="" />
</form>
</div>
</div>
<div id="div_form" align="center" style="display: none;">
<form action="" class="layui-form">
<div style="margin-top: 15px;text-align: right;margin-right: 45px;">
<input type="hidden" id="exportReportOrgid" >
<button class="layui-btn" οnclick="exportReport()">导出报表</button>
</div>
<div class="layui-form">
<table class="layui-table" >
<caption style="font-size: 18px;" id="titleReport"></caption>
<caption style="font-size: 14px;text-align: right;margin-right: 20px;" id="timeReport"></caption>
<thead>
<tr>
<th>计划名称</th>
<th>企业名称</th>
<th>检查事由</th>
<th>检查结果</th>
<th>整改情况</th>
<th>负责人</th>
<th>督办人</th>
<th>督办负责人</th>
<th>整改意见</th>
<th>整改结果</th>
<th>验收情况</th>
<th>行政处罚</th>
<th>案卷号</th>
</tr>
</thead>
<tbody id="report">
<!-- <tr>
<td rowspan="2">2019年安全生产监督检查计划</td>
<td>安河桥有限公司</td>
<td>根据检查计划进行检查</td>
<td>发现了企业存在不安全记录</td>
<td>整改中</td>
<td>张三</td>
<td>张三</td>
<td>张三是</td>
<td>增加灭火器</td>
<td>符合整改情况</td>
<td>合格</td>
<td>已处罚</td>
<td>1201254852</td>
</tr> -->
</tbody>
</table>
</div>
</form>
</div>
-- js --
//导出 Excel数据
var exports = function(){
var url = basePath + '/jd/situationanalysis/sendout';
var formData = com.weikun.bfw.common.form.serializeForm('searchform');
$.ajax({
type:"post",
url:url,
data:formData,
dataType:"json",
complete:function(res){
console.log(res.responseText);
if(res.status == "200"){
var url1 = basePath + '/jd/situationanalysis/download?name='+res.responseText;
window.location.href = url1;
}
}
});
}
//导出 Excel数据 某个组织机构的报表
var exportReport = function(){
var name;
var url = basePath + '/jd/situationanalysis/sendoutByOrg';
var formData = com.weikun.bfw.common.form.serializeForm('searchform');
var orgid = $('#exportReportOrgid').val();
formData.sm_eq_orgid = orgid;
$.ajax({
type:"post",
url:url,
data:formData,
async:false,
dataType:"json",
complete:function(XMLHttpRequest, textStatus){
if(textStatus == "success"){
name = XMLHttpRequest.responseText;
name = name.substring(1,name.length-1);
}
}
});
window.location.href = basePath + '/jd/situationanalysis/downloadByOrg?orgid='+orgid+'&name='+name;
window.event.returnValue=false;
}
-- Action --
/**
* 导出数据
* @param sm
* @param model
* @param session
* @param request
* @throws Exception
*/
@RequestMapping("sendout")
@ResponseBody
public String sendout(SituationanalysisSM sm, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
String msg = "OK";
long count = 0l;
String name = "";
List<Situationanalysis> list = new ArrayList<Situationanalysis>();
try {
list = situationanalysisService.listAll(sm, null, model);
count = (long) list.size();
//生成excel并下载
name = situationanalysisService.createExcel(sm,list,request,response);
} catch (Exception e) {
e.printStackTrace();
msg = e.getMessage();
}
return name;
}
/**
* 导出数据
* @param sm
* @param model
* @param session
* @param request
* @throws Exception
*/
@RequestMapping("sendoutByOrg")
@ResponseBody
public void sendoutByOrg(SituationanalysisSM sm, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
String msg = "OK";
long count = 0l;
String name = "";
List<Situationanalysis> list = new ArrayList<Situationanalysis>();
try {
list = situationanalysisService.listByOrgid(sm, model);
count = (long) list.size();
//生成excel并下载
name = situationanalysisService.createExcelByOrg(sm, list, request, response);
response.setContentType("text/html;charset=UTF-8");//这些设置必须要放在getWriter的方法之前,
response.getWriter().print(JSON.toJSONString(name));
} catch (Exception e) {
e.printStackTrace();
msg = e.getMessage();
}
}