公司-->ajax获取数据

本文介绍了一个基于Ajax的JavaScript函数,该函数通过发送POST请求到特定的URL来获取与给定工段ID相关的部门列表,并将这些部门信息填充到指定的下拉列表中。此外,还详细介绍了后端实现的Action类以及具体的业务逻辑实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js内容:

/**通过工段ID获取部门列表*/
function getDepartmentListBySectionIdFun(){
	var sectionId=$("#sectionId").val();
	var defvalue = $("#departmentId").attr("defvalue");
	$("#departmentId").empty();
	$.ajax({
		type : "post",
		url : contentPath + "/ajax/getDepartmentListBySectionId.do",
		data : {
			sectionId : sectionId
		},
		async : false,
		dataType : "json",
		success : function(data) {
			if (data) {
				var arr=eval(data);
				for(var i = 0; i < arr.length; i++) {
					$("#departmentId").append("<option value = '"+arr[i].departmentId+"'>"+arr[i].departmentName+"</option>");
				}
				if(defvalue){
					$("#departmentId").find("option[value = '"+defvalue+"']").attr("selected","selected");
				}
			}
		}
	});
	showIePHAndIePersons();
}


action内容:

package gts.erp.action.ajax;

import gts.erp.action.base.ERPProxyAction;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.oletech.triangle.component.CellBean;
import com.oletech.triangle.component.FormBean;
import com.oletech.triangle.utils.TriangleUIHelper;

/**
 * 
 * 通过工段ID获取部门列表
 * @author ole
 *
 */
public class GetDepartmentListBySectionIdAction extends ERPProxyAction {

	@Override
	protected ActionForward doExecute(FormBean parameterFB, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		FormBean formBean = this.getERPServiceDelegation().getMasterProductionScheduleService().getDepartmentBySection(parameterFB);
		
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < formBean.size(); i++) {
			CellBean cellBean = formBean.get(i);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("departmentId", cellBean.getKey());
			jsonObject.put("departmentName", TriangleUIHelper.getMessageResources(request, cellBean.getValue()));
			jsonArray.add(jsonObject);
		}
		PrintWriter pw = response.getWriter();
		pw.print(jsonArray);
		pw.flush();
		return null;
	}

}


impl内容:

/** 
	 * 根据工段查询部门  
	 * @author zhuyz 
	 * @date 2016年11月19日17:45:16
	 * @param dataBean
	 * 			FormBean => key : "parameterFB"
	 *          	 CellBean key : sectionId
	 * @return FormBean
	 * 				FormBean Key: errorMsg
	 *  			TableBean => key : "TN_DEPARTMENT"
	 *         		       CellBean key : CN_ID,CN_NAME..........
	 *              TableBean => key : "TN_DEPARTMENT_IESETUP"
	 *                     CellBean key :  CN_ID,CR_DEPARTMENT_ID......                    
	 */
	public FormBean getDepartmentBySection(FormBean formBean) {
		String sectionId = formBean.getCellBeanValue("sectionId");
		FormBean returnFB = new FormBean();
		try {
			if (StringUtils.isNotEmpty(sectionId)) {
				CondSetBean csb = new CondSetBeanJustAnd();
				csb.addCondBean(new CondBeanEqual("CR_SECTION_ID", sectionId));
				TableBean tableBean = this.baseDAO.queryForTableBean(new ClassPOJO("TN_DEPARTMENT_IESETUP"), csb);
				CondSetBean csbDepartment = new CondSetBeanJustAnd();
				CondBeanIn cbiDepartment = new CondBeanIn(TriangleDefinition.COLUMN_NAME_CN_ID);
				for (int i = 0; i < tableBean.size(); i++) {
					cbiDepartment.addValue(tableBean.get(i).getCellBeanValue("CR_DEPARTMENT_ID"));
				}
				if (cbiDepartment.size() > 0) {
					csbDepartment.addCondBean(cbiDepartment);
					TableBean departmentTB = this.baseDAO.queryForTableBean(new ClassPOJO("TN_DEPARTMENT"), csbDepartment);
					for (int i = 0; i < departmentTB.size(); i++) {
						RowBean rowBean = departmentTB.get(i);
						returnFB.addCellBean(new CellBean(rowBean.getCellBeanValue(TriangleDefinition.COLUMN_NAME_CN_ID), rowBean
								.getCellBeanValue(TriangleDefinition.COLUMN_NAME_CN_NAME)));
					}
				}
			}
		} catch (Exception e) {
			TriangleBLHelper.printExceptionLog(log, e);
			e.printStackTrace();
			throw new RuntimeException("**** Run time Exception!****");
		}
		return returnFB;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值