后端可以用StringBuffer拼接html代码,注意拼接过程中小心使用转义符,之前做项目的时候就是用了转义符的原因导致前端拿不到数据
一、后端拼接代码:
private StringBuffer getTable() throws ParseException {
ITfmUserProfile user = getCurrentUser();
StringBuffer sb = new StringBuffer();
if(user.getRoleId().equals("xxxx")){
// 获取本系统当前用户
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -15);
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
DateFormat formatTarget = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startTime = format.format(cal.getTime());
startTime = startTime.substring(0,(startTime.length() - 6))+ "000000";
String endTime = format.format(date.getTime());
String deptId = user.getOrgId();
List<TfAccountVO> vos = (List<TfAccountVO>)super.getBasService().queryHiber("from TfAccountVO where deptId = ? and accountTypeCode = '1000'", new Object[]{deptId});
//表格
sb.append("<table width='100%'>");
for (TfAccountVO tfAccountVO : vos) {
List<TfBankTradeInfoVO> tfBankTradeInfoVOs = (List<TfBankTradeInfoVO>)super.getBasService().queryHiber("from TfBankTradeInfoVO where account_no = ? and trade_type = '0' and trade_time BETWEEN ? and ? order by trade_time desc", new Object[]{tfAccountVO.getAccountNo(),startTime,endTime});
if(tfBankTradeInfoVOs.size() > 0 ){
sb.append("<tr>").append("<td style='padding-left: 20px;'>").append(tfAccountVO.getAccountAlias()).append("尾号为").append(tfAccountVO.getAccountNo().substring(tfAccountVO.getAccountNo().length() - 4)).append("的账户").append("</td>").append("</tr>");
}
for (TfBankTradeInfoVO tfBankTradeInfoVO : tfBankTradeInfoVOs) {
sb.append("<tr>").append("<td style='padding-left: 20px;'>").append("于").append(formatTarget.format(format.parse(tfBankTradeInfoVO.getTradeTime()))).append(" ").append("发生一笔金额为").append(tfBankTradeInfoVO.getTradeAmount()).append("元的收入,").append("</td>").append("</tr>");
}
}
sb.append("<tr>").append("<td style='padding-left: 20px;'>").append("请及时处理,如该笔收入无需处理,请忽略!").append("</td>").append("</tr>");
sb.append("</table>");
}
return sb;
}
二、前端接受数据:
var cmd = $J("Command_GetTable",namespace);
var table = cmd.parameters.getValue("table");
if(table.indexOf("尾号为") != -1){
document.getElementById("showTable").style.display="";
document.getElementById('bankFlow').innerHTML = table;
}