js数组 var exportRecords=[]; exportRecords.push(record);
{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"}
{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}
在后台直接用对应属性拼接的model并不能接收到,要先转化成json数组。
错误写法:
var json ={};
for(var i=0;i<exportRecords.length;i++){
json[i]=exportRecords[i];
}
param:
data:JSON.stringify(json);
只能得到
{"0":{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"},"1":{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}}
还是不能用modelList接收到。
正确写法:
要获得json数组的数据格式应该是:
[{"id":"201507172057021430000","monitorDate":"2015-05-14","dId":"Daily-1"},{"id":"201507161702461070000","monitorDate":"2015-07-09","dId":"Daily-2"}]
完整代码如下:(包含extjs的checkbox勾选项获取办法)
function exportExcel() {
//先获取选择模型,然后从选择模型中获取选中的记录
var selections = grid.getSelectionModel().getSelection();
var exportRecords=[];
var json = [];
var sendData="";
if (selections.length > 0) {
Ext.Msg.confirm('提示', '你确认导出选中的记录吗?', function(_btn) {
if (_btn == 'yes') {
for (var i = 0; i < selections.length; i++) {
var record = selections[i].data;
exportRecords.push(record);
}
for(var i=0;i<exportRecords.length;i++)
{
sendData=sendData+JSON.stringify(exportRecords[i])+",";//转成json格式字符串
}
sendData=sendData.substr(0,sendData.length-1);//去掉最后一个逗号,
sendData="["+sendData+"]";//拼接中括号[]
//alert(sendData);
if(Ext.getCmp('writeStartDate').getValue()!=null)
writeStartDate=Ext.getCmp('writeStartDate').getValue();
if(Ext.getCmp('writeEndDate').getValue()!=null)
writeEndDate=Ext.getCmp('writeEndDate').getValue();
Ext.MessageBox.wait('正在导出测点Excel文档', '请稍候...');
Ext.Ajax.request({
url : 'action.ReportManage.ProjectReportAction.do?method=exportRptDailyExcel',
params : {
'data' : sendData,
'pId' : projectId,
'writeStartDate' :writeStartDate,
'writeEndDate' : writeEndDate
},
success : function(response, opts) {
Ext.MessageBox.hide();
var fp = Ext.decode(response.responseText).data;
window.open('../../../' + fp,'_self'); // 下载Excel导出文件
}
});
}
});
}else{
Ext.Msg.alert('提示', '请勾选要导出的记录');
}
}
后台:
List<RptDailyRecordData> exportRecordsList=ActionUtil.getReqContentWithList(RptDailyRecordData.class);
public class RptDailyRecordData {
private String dId;
private String id;//日报id
private String monitorDate;
private Integer mmId;
private String mmName;//监测项目
private int index;
private int issue;
private String pStatusReport;
private String path;
private String pmId;
private String pAddress;
private String writeDate;
private String writePerson;//监测人员
private String comment;//备注
private String analysis;//概要分析
//省略无参和有参的构造函数,还有getter,setter方法
}
/**
* 将前台传过来的json串转化为clazz类型的链表
*/
public static <T> List<T> getReqContentWithList(Class<T> clazz) {
try {
return (List<T>) JsonUtil.json2List(ActionContext.getRequest()
.getParameter("data"), clazz);
} catch (Exception e) {
log.error("将前台数据封装为bean对象的链表时出错", e);
throw new RuntimeException("将前台数据封装为bean对象的链表时出错");
}
}
/**
* json字符串转换为javaBean的链表
*
*/
@SuppressWarnings("unchecked")
public static <T> List<T> json2List(String json, Class<T> beanClass) {
try {
json = json.replaceAll("(\\d{4}-\\d{2}-\\d{2})(T)", "$1 ");
ObjectMapper objectMapper = getMapperInstance();
objectMapper.setDateFormat(myFormat);
return (List<T>) objectMapper.readValue(json,
getCollectionType(List.class, beanClass));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("json字符串转化为List对象出错");
}
}