前台到后台的交互,至成功导出Excel

前台 easyui datagrid控件

var dg;
var d;
$(function(){   
dg=$('#dg').datagrid({    
method: "get",
//   url:'${ctx}/system/user/json', 
   url:'${ctx}/business/serverManage/json',
   fit : true,
fitColumns : true,
border : false,
idField : 'serviceId',
striped:true,
pagination:true,
rownumbers:true,
pageNumber:1,
pageSize : 20,
pageList : [ 10, 20, 30, 40, 50 ],
   columns:[[    
{field:'ck',checkbox:true},
// { field:'productid',width:100,align:'center',checkbox:true }, 
{field:'artNum',title:'病历号',width:100,align:'center'},
       {field:'artcyNum',title:'周期号',width:100,align:'center'}

       
      , { field:'femaleIdNum',title:'女方证件号',width:150,align:'center',
      formatter:function(value,rows){
      return value.substring(0,8)+"********"+value.substring(16);
      }},
       { field:'maleIdNum',title:'男方证件号',width:150,align:'center',
      formatter:function(value,rows){
      return value.substring(0,8)+"********"+value.substring(16);
      }}
       


     ,{field:'artType',title: '技术类别' , width:108}
     
       
       ,{ field:'attr1',title:'填报人',width:100,align:'center'},
       {field:'reportStatus',title:'状态',width:100,align:'center'}
       

   ]], 
   /* headerContextMenu: [
       {
           text: "冻结该列", disabled: function (e, field) { return dg.datagrid("getColumnFields", true).contains(field); },
           handler: function (e, field) { dg.datagrid("freezeColumn", field); }
       },
       {
           text: "取消冻结该列", disabled: function (e, field) { return dg.datagrid("getColumnFields", false).contains(field); },
           handler: function (e, field) { dg.datagrid("unfreezeColumn", field); }
       }
   ], */
   enableHeaderClickMenu: true,
   enableHeaderContextMenu: true,
   enableRowContextMenu: false,
   toolbar:'#tb'
});
});


//弹窗修改
function edit(){
$.ajaxSetup({type : 'GET'});
var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
// window.open("${ctx}/business/serverManage/update/"+row.serviceId ,"修改",'height=500,width=1200,top=100,left=100,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no') ;


d=$("#dlg").dialog({   
   title: '修改',    
//    width: 1100,    
//    height: 450, 
   fit:true,
   href:"${ctx}/business/serverManage/update/"+row.serviceId,
   maximizable:true,
   modal:true//,
});
}

//删除
function del(){
var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除?', function(data){
if (data){
$.ajax({
type:'get',
url:"${ctx}/business/serverManage/delete/"+row.serviceId,
success: function(data){
successTip(data,dg);
}
});

});
}


//删除多个
function delM(){
var idList=[];
var data=dg.datagrid('getSelections');
debugger;
for(var i=0,j=data.length;i idList.push(data[i].serviceId);
}

var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除?', function(data){
if (data){
$.ajax({
type:'POST',
url:"${ctx}/business/serverManage/delete",
data:JSON.stringify(idList),
contentType:'application/json;charset=utf-8',
success: function(data){
if(data=='success'){
dg.datagrid('reload');
dg.datagrid('clearSelections');
parent.$.messager.show({ title : "提示",msg: "操作成功!", position: "bottomRight" });
} else{
parent.$.messager.alert('提示',data);

}
});
//dg.datagrid('reload'); //grid移除一行,不需要再刷新

});
}


//上报
function report(){
var idList=[];
var data=dg.datagrid('getSelections');
debugger;
for(var i=0,j=data.length;i idList.push(data[i].serviceId);
}

var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
parent.$.messager.confirm('提示', '是否确定上报?', function(data){
if (data){
$.ajax({
type:'POST',
url:"${ctx}/business/serverManage/report",
data:JSON.stringify(idList),
contentType:'application/json;charset=utf-8',
success: function(data){
if(data=='success'){
dg.datagrid('reload');
dg.datagrid('clearSelections');
parent.$.messager.show({ title : "提示",msg: "操作成功!", position: "bottomRight" });
} else{
parent.$.messager.alert('提示',data);

}
});
//dg.datagrid('reload'); //grid移除一行,不需要再刷新

});
}
//申请修改
function applyUpdate(){
var idList=[];
var data=dg.datagrid('getSelections');
debugger;
for(var i=0,j=data.length;i idList.push(data[i].serviceId);
}

var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
parent.$.messager.confirm('提示', '是否确定上报?', function(data){
if (data){
$.ajax({
type:'POST',
url:"${ctx}/business/serverManage/applyUpdate",
data:JSON.stringify(idList),
contentType:'application/json;charset=utf-8',
success: function(data){
if(data=='success'){
dg.datagrid('reload');
dg.datagrid('clearSelections');
parent.$.messager.show({ title : "提示",msg: "操作成功!", position: "bottomRight" });
} else{
parent.$.messager.alert('提示',data);

}
});
//dg.datagrid('reload'); //grid移除一行,不需要再刷新

});
}
//弹窗查看
function look(){
$.ajaxSetup({type : 'GET'});
var row = dg.datagrid('getSelected');
if(rowIsNull(row)) return;
//window.open("${ctx}/business/serverManage/check/"+row.serviceId ,"查看",'height=500,width=1200,top=100,left=100,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no') ;
d=$("#dlg").dialog({   
   title: '查看',    
//    width: 1100,    
//    height: 450, 
   fit:true,
   href:"${ctx}/business/serverManage/check/"+row.serviceId,
   maximizable:true,
   modal:true//,
});
}

function ddd (abc){
var row = dg.datagrid('getSelected');
row.serviceId;
debugger;
if(rowIsNull(row)) return;
$.ajax({
type:'get',
url:"${ctx}/business/serverManage/update/"+row.serviceId,
success: function(data){
data;
debugger;
$("html").html(data);
}
});
}


//创建查询对象并查询
function cx(){
var obj=$("#searchFrom").serializeObject();

dg.datagrid('load',obj);
var rows  = $('#dg').datagrid("getRows");
debugger;
}




//导出excel
function exportExcel1(){
var url = "${ctx}/business/serverManage/exportExcel";
window.location.href = url;
}


//导出excel
function exportExcel2(){
var rows  = $('#dg').datagrid("getRows");
var rows_json=JSON.stringify(rows);
var fields=$("#dg").datagrid('getColumnFields',false);
fields.remove(0);
var  fields_json=JSON.stringify(fields);
var titles=$("#dg").datagrid('getColumns',false);
var columns=[];
for(var i=1;i columns.push(titles[i].title);
}
var columns_json=JSON.stringify(columns);
debugger;
var fileName="周期信息表";
$.post("${ctx}/business/serverManage/exportExcel", {rows:rows_json, fields:fields_json, columns:columns_json, fileName:fileName}, 
function(data){

debugger;
 },
 "text");
}

function exportExcel(){
var rows  = $('#dg').datagrid("getRows");
var rows_json=JSON.stringify(rows);
var fields=$("#dg").datagrid('getColumnFields',false);
fields.remove(0);
var  fields_json=JSON.stringify(fields);
var titles=$("#dg").datagrid('getColumns',false);
var columns=[];
for(var i=1;i columns.push(titles[i].title);
}
var columns_json=JSON.stringify(columns);
var realURL="${ctx}/business/serverManage/exportExcel";
//realURL += "?queryCode=QRY_ZRQK_GJ&&rows=9999&&orderByColumn=AREAID&&orderByType=asc&&fileName=ziranqingkuang";
var iframe = document.getElementById("downLoaderUtilsIframeV110912"); // 创建iframe
if(iframe==null){
iframe= document.createElement('iframe');
iframe.setAttribute('src','javascript:false');
iframe.setAttribute('id',"downLoaderUtilsIframeV110912");
iframe.width=0;
iframe.height=0;
document.body.appendChild(iframe);
}
var form = document.createElement('form'); // 创建form
form.action=realURL;
form.method="POST";
var cfg ={
// columns: encodeURIComponent(JsonArrayToString(columns)),
rows:rows_json,
columns: columns_json,
fields:fields_json,
page : 1,
fileName:'治疗周期表'//,
};
for(var p in cfg){
if(p){
var ipts = document.createElement('input');
ipts.setAttribute('type', 'hidden');
ipts.setAttribute('name',p);
ipts.value=cfg[p];
form.appendChild(ipts);
}
}
iframe.appendChild(form);
if (window.attachEvent) {
iframe.attachEvent('onload',function(){
document.body.removeChild(iframe);
});
} else {
iframe.addEventListener('load',function(){
document.body.removeChild(iframe);
}, false);
}
form.submit();
};

function JsonArrayToString(jsonArray){
  var JsonArrayString = "[";
  for(var i=0;i   JsonArrayString=JsonArrayString+json2str(jsonArray[i])+",";
  }
  JsonArrayString = JsonArrayString.substring(0,JsonArrayString.length-1)+"]";
  return JsonArrayString;
};

function json2str(o) {
   var arr = [];
   var fmt = function(s) {
       if (typeof s == 'object' && s != null) return json2str(s);
       return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    };
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
 };

利用 post方法想后台发送请求,并传值

//导出excel
function exportExcel(){
var rows  = $('#dg').datagrid("getRows");
var rows_json=JSON.stringify(rows);
var fields=$("#dg").datagrid('getColumnFields',false);
fields.remove(0);
var  fields_json=JSON.stringify(fields);
var titles=$("#dg").datagrid('getColumns',false);
var columns=[];
for(var i=1;i columns.push(titles[i].title);
}
var columns_json=JSON.stringify(columns);
debugger;
$.post("${ctx}/business/serverManage/exportExcel/11",{rows:rows_json,fields:fields_json,columns:columns_json},
 function(data){
debugger;
 },
 "text");
}


@RequestMapping(value = "exportExcel")
@ResponseBody
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
String rows=(String) request.getParameter("rows");//使用getParameter() 方法
String fields=(String) request.getParameter("fields");
String columns=(String) request.getParameter("columns");

System.out.println(rows);
System.out.println(fields);
System.out.println(columns);



未完,待整理,待续。。。


//导出excel
function exportExcel1(){
var url = "${ctx}/business/serverManage/exportExcel";
window.location.href = url;
}


//导出excel
function exportExcel2(){
var rows  = $('#dg').datagrid("getRows");
var rows_json=JSON.stringify(rows);
var fields=$("#dg").datagrid('getColumnFields',false);
fields.remove(0);
var  fields_json=JSON.stringify(fields);
var titles=$("#dg").datagrid('getColumns',false);
var columns=[];
for(var i=1;icolumns.push(titles[i].title);
}
var columns_json=JSON.stringify(columns);
debugger;
var fileName="周期信息表";
$.post("${ctx}/business/serverManage/exportExcel", {rows:rows_json, fields:fields_json, columns:columns_json, fileName:fileName}, 
function(data){

debugger;
 },
 "text");
}



//导出
function exportExcel(){
var rows  = $('#dg').datagrid("getRows");
var rows_json=JSON.stringify(rows);
var fields=$("#dg").datagrid('getColumnFields',false);
fields.remove(0);
var  fields_json=JSON.stringify(fields);
var titles=$("#dg").datagrid('getColumns',false);
var columns=[];
for(var i=1;icolumns.push(titles[i].title);
}
var columns_json=JSON.stringify(columns);
var realURL="${ctx}/business/serverManage/exportExcel";
//realURL += "?queryCode=QRY_ZRQK_GJ&&rows=9999&&orderByColumn=AREAID&&orderByType=asc&&fileName=ziranqingkuang";
var iframe = document.getElementById("downLoaderUtilsIframeV110912");// 创建iframe
if(iframe==null){
iframe= document.createElement('iframe');
iframe.setAttribute('src','javascript:false');
iframe.setAttribute('id',"downLoaderUtilsIframeV110912");
iframe.width=0;
iframe.height=0;
document.body.appendChild(iframe);
}
var form = document.createElement('form');// 创建form
form.action=realURL;
form.method="POST";
var cfg ={
// columns: encodeURIComponent(JsonArrayToString(columns)),
rows:rows_json,
columns: columns_json,
fields:fields_json,
page : 1,
fileName:'治疗周期表'//,
};
for(var p in cfg){
if(p){
var ipts = document.createElement('input');
ipts.setAttribute('type', 'hidden');
ipts.setAttribute('name',p);
ipts.value=cfg[p];
form.appendChild(ipts);
}
}
iframe.appendChild(form);
if (window.attachEvent) {
iframe.attachEvent('onload',function(){
document.body.removeChild(iframe);
});
} else {
iframe.addEventListener('load',function(){
document.body.removeChild(iframe);
}, false);
}
form.submit();
};

function JsonArrayToString(jsonArray){
  var JsonArrayString = "[";
  for(var i=0;i  JsonArrayString=JsonArrayString+json2str(jsonArray[i])+",";
  }
  JsonArrayString = JsonArrayString.substring(0,JsonArrayString.length-1)+"]";
  return JsonArrayString;
};

function json2str(o) {
   var arr = [];
   var fmt = function(s) {
       if (typeof s == 'object' && s != null) return json2str(s);
       return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    };
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
 };

你可能感兴趣的:(jquery)