Ajax實現子頁面資料帶到父頁面
1.父頁面:
此list資料需從子頁面帶過來,查詢出來為一個list,需裝換為json。
2.js
//發起核查
function getInspect(){
var flowId = $("#flowId").val();
var injureId = $("#injureId").val();
var taskType = $("#taskType").val();
var url = path+"/inspect/doStartInspectFlow.do?injureId="+injureId+"&taskTypes="+taskType+"&flowId="+flowId;
window.open(url,"","dialogWidth=1020px,dialogHeight=700px,dialogLeft=100px,dialogTop=120px,resizable=yes,scrollbars=yes");
}
3.struts
<!-- 發起任務 -->
<action name="doStartInspectFlow" class="CmInspectAction" method="doStartInspectFlow">
<result>/jsp/inspect/start_inspect.jsp</result>
</action>
4.action
public String doStartInspectFlow() throws Exception {
flowId = this.getParameter("flowId");
//用於判斷子頁面執行父頁面js
String backfull = this.getParameter("backfull");
injureId = this.getParameter("injureId");
List list = (List) cmCommonService.getDictListByCode("D202").get("D202");
flows = cmInspectService.getListInspectFlowDTO(injureId);
/*for (int i = 0; i < list.size(); i++) { // 任務列表中移除已發起的任務
MdDictElement e = (MdDictElement) list.get(i);
if (flows == null || flows.size() == 0) {
taskTypeList = list;
break;
}
boolean contains = false;
for (int j = 0; j < flows.size(); j++) {
flows.get(j).setInitiatorDate(flows.get(j).getInitiatorDate().substring(0, 19));
if (flows.get(j).getTaskType().equals(e.getDictCode())) {
contains = !contains;
}
}
if (!contains) {
taskTypeList.add(e);
}
}*/
taskTypeList=list;
if(backfull!=null){
setAttribute("backfull", "backfull");
}
return Constant.SUCCESS;
}
5.子頁面儲存js
function save() {
var handlername=$("#handlerName").val();
if(handlername.length>0){
if ($(":checked").size() > 0) {
var audiname=$("#treeorg").val();
$("form:first").attr("action", "<%=path%>/inspect/doAddInspectFlow.do?autiname="+audiname); //執行action
$("form:first").submit();
}else{
alert("請選擇任務型別!");
}
}else{
alert("請選擇任務接收人!");
}
}
<c:if test="${backfull=='backfull'}">
$(function(){
window.parent.opener.document.getElementById("backfullval").value='${injureId}';
window.parent.opener.backfull(); //父頁面執行ajax方法
});
</c:if>
6.父頁面執行ajax方法
//發起核查-從子頁面重新整理到父頁面
function backfull(){
var injureId=$("#backfullval").val();
$.ajax({
type: "POST",
async:false,
url: getRootPath()+"/inspect/doLoadInspectInform.do?injureId="+injureId,
success: function(data){
var str='';
$("#backfull").html("");
for(var i=0;i<data.length;i++){
var str1='';
if(data[i].taskState==1){
str1='待核查';
}else if(data[i].taskState==2){
str1='待稽核';
}else if(data[i].taskState==3){
str1='稽核完成';
}
str+='<tr>';
str+=' <td style="width: 25%;">';
str+= data[i].taskName;
str+=' </td>';
str+=' <td style="width: 25%;">';
str+= new Date(data[i].initiatorDate).format('yyyy-MM-dd hh:mm:ss');
str+=' </td>';
str+=' <td style="width: 25%;">';
str+= str1;
str+=' </td>';
str+=' <td style="width: 25%;">';
str+=' <input type="button" onclick="toDetail(\''+data[i].id+'\',\''+data[i].injureId+'\',\''+data[i].taskType+'\');" class="button" value="詳細"/>';
str+=' </td>';
str+='</tr>';
}
$("#backfull").append(str);
$("#InspectFlowId").show();
if ($("#InspectFlowId").find(".list-data-show").is(":hidden")) {
$("#InspectFlowId").find(".title").click();
}
}
});
};
7.ajax action
public void doLoadInspectInform() throws Exception {
HttpServletRequest request=getRequest();
String injureId = request.getParameter("injureId");
List inspectList = cmInspectService.getListInspectFlowDTO(injureId);
HttpServletResponse response = getResponse();
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(JSON.toJSONString(inspectList).toString());
out.flush();
out.close();
}
8.附加
//JS格式化時間 毫秒-24小時
Date.prototype.format =function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds() //second
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for ( var k in o)
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]: ("00" + o[k]).substr(("" + o[k]).length));
return format;
};
9.子頁面: