json实现部门二级联动

5 篇文章 0 订阅
5 篇文章 0 订阅
 function getEmp(){
           var deparementids =$("#deparementid").val();
            $.ajax({
   url: '<%=request.getContextPath()%>/contractAction_departmentuser.action?deparementids='+deparementids,  
   type: "post",
   dataType: "xml",
   timeout: 3000,
   async : false,
   error: function(){
   alert(数据加载错误);   
  },
  success: function(xml){
  $("#employeeid").empty();
  $("#xm").append("<option value=\"\">---请选择---</option>");
  $(xml).find("result").each(function(){
   var employeeid = $(this).find("employeeid").text();
var employeename = $(this).find("employeename").text();
$("#employeeid").append("<option value="+employeeid+">"+ employeename +"</option>");
$("#employeename").val(employeename);
});
}
    });

  }


JSP

<td height="25" bgcolor="#EEEEEE" width="10%">
<div align="center" class="STYLE1">
<div align="center">合同签订部门</div>
</div>
</td>
<td height="25" bgcolor="#FFFFFF" width="10%">
<div align="center" class="STYLE1">
<div align="center">
 <select name="deparementid" id="deparementid"  οnchange="getEmp()" style="width:60%; margin:auto; outline:none;">
                                                   <option value="">请选择</option>
                                                 <c:forEach items="${deparementList}" var="p" varStatus="i">
                                                   <option value="${p.id}">${p.name}</option>
                                                </c:forEach>
                                             </select>
</div>
</div>
</td>
<td height="25" bgcolor="#EEEEEE" width="10%">
<div align="center" class="STYLE1">
<div align="center">合同签订人</div>
</div>
</td>
<td height="25" bgcolor="#FFFFFF" width="10%">
<div align="center" class="STYLE1">
<div align="center">
<select name="employeeid"  id="employeeid"   style="width:60%; margin:auto; outline:none;">
                                                  <option value="">---请选择---</option>
                                                 </select>
</div>
</div>
</td>



controller层

private Long deparementids;// 获取部门id,根据部门id,二级菜单联动出部门下的员工
private HttpServletResponse response;// 设置xml格式

public Long getDeparementids() {
return deparementids;
}


public void setDeparementids(Long deparementids) {
this.deparementids = deparementids;
}


public HttpServletResponse getResponse() {
return response;
}


public void setResponse(HttpServletResponse response) {
this.response = response;
}

/**
* 部门员工二级菜单联动,根据传来的部门id查找出部门下的员工信息,把这个员工结果集返回到添加页面

* @throws Exception
*/
public void departmentuser() throws Exception {
List<User> getPersonnel = userService.getBydeparementId(deparementids);
PrintWriter out = null;
response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");// 指定返回值的编码方式,必须放在out声明之前
try {
out = response.getWriter();
response.setContentType("text/xml");
out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.print("<results>");
for (User bean : getPersonnel) {
out.print("<result>");
out.print("<employeeid>");
out.print(bean.getId());
out.print("</employeeid>");
out.print("<employeename>");
out.print(bean.getName());
out.print("</employeename>");
out.print("</result>");
}
out.print("</results>");
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值