jquery

===================================================== 一、jsp页面部分代码 =====================================================

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type='text/javascript' src='../js/jquery.js'></script>

<script type='text/javascript'>
var parent_id="<s:property value="user.suboffice_id"/>";
var child_id="<s:property value="user.section_id"/>";
$(document).ready(function(){
query_dept_list();
});
//获取上级部门列表
function query_dept_list(){
$.ajax({
type: "GET",
url: "query_deptno_list.action?set="+new Date(),
dataType: "json",
success:function (data) {
var json=eval(data);
for(var i = 0,len = json.length; i<len; i++){
$('#deptnameChild').empty();
$('#deptnameChild').append("<option value=\"0\">---请选择子部门---</option>");
if(parent_id==json[i].departid)
$('#deptnameParent').append($("<option value='"+json[i].departid+"' selected>"+json[i].departname+"</option>"));
else
$('#deptnameParent').append($("<option value='"+json[i].departid+"'>"+json[i].departname+"</option>"));
}
$('#deptnameParent').trigger("change");
}
});
//当上级部门选项发生变化
$('#deptnameParent').change(function(){
$('#deptnameChild').empty();
$('#deptnameChild').append("<option value=\"0\">---请选择子部门---</option>");
//当上级部门选项发生变化时,其对应的子部门也随着改变
query_dept_child_list();
});
}

function query_dept_child_list(){
//获取选中的上级部门的编号departid
var deptnameParent = document.getElementById('deptnameParent').value;
if (deptnameParent=="0") {
$('#deptnameChild').empty();
$('#deptnameChild').append("<option value=\"0\">---请选择子部门---</option>");
}else {
$.ajax({
type: "GET",
url: "query_dept_child_list.action?pid="+deptnameParent,
dataType: "json",
success:function (data) {
var json=eval(data);
for(var i = 0,len = json.length; i<len; i++){
if(child_id == json[i].departid)
$('#deptnameChild').append($("<option value='"+json[i].departid+"' selected>"+json[i].departname+"</option>"));
else
$('#deptnameChild').append($("<option value='"+json[i].departid+"'>"+json[i].departname+"</option>"));
}
}
});
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="update_user.action">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="10%" height="20" class="STYLE6">
<div align="right">
<span class="STYLE10">部门:</span>
</div>
</td>
<td noWrap width="32%">
<select name="user.suboffice_id" id="deptnameParent" style="width: 155px;">
<option value="0">
---请选择父部门---
</option>
</select>
  
<select name="user.section_id" id="deptnameChild" style="width: 155px;">
<option value="0">
---请选择子部门---
</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>

===================================================== 二、struts.xml部分代码 =====================================================
<struts>
<package name="user" extends="struts-default" namespace="/user">
<action name="update_user" class="com.moa.user.UserAction" method="update_user">
<result name="success">user_list.action</result>
<result name="fail">user_list.action</result>
</action>
</package>
</struts>

===================================================== 三、ImeiAction.java部分代码 =====================================================
/*
* 获取上级部门及其子部门列表返回给jsp
*/
public String query_deptno_list() throws Exception {
ImeiHandle imeiHandle = new ImeiHandle();
// 获取上级部门及其子部门列表
List<SysDepartmentModel> deptList = imeiHandle.getDeptPCList();
JSONArray jsonArray = JSONArray.fromObject(deptList);
PrintWriter writer = null;
try {
writer = ServletActionContext.getResponse().getWriter();
} catch (IOException e) {
e.printStackTrace();
}
writer.write(jsonArray.toString());
writer.flush();
if (writer != null) {
writer.close();
}
return null;
}

/*
* 获取上级部门编号为cid的子部门列表返回给jsp
*/
public String query_dept_child_list() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String pid = request.getParameter("pid");
ImeiHandle imeiHandle = new ImeiHandle();
// 获取获取上级部门编号为cid的子部门列表
List<SysDepartmentModel> deptList = imeiHandle.getDeptCList(pid);
JSONArray jsonArray = JSONArray.fromObject(deptList);
PrintWriter writer = null;
try {
writer = ServletActionContext.getResponse().getWriter();
} catch (IOException e) {
e.printStackTrace();
}
writer.write(jsonArray.toString());
writer.flush();
if (writer != null) {
writer.close();
}
return null;
}
}

===================================================== 四、ImeiHandle.java部分代码 =====================================================

/**
* 获取上级部门列表
*/
public List<SysDepartmentModel> getDeptPCList() {
// 上级部门的集合
List<SysDepartmentModel> sysdepartmentList = new ArrayList<SysDepartmentModel>();
String sql = "select DEPARTID,DEPARTNAME,SUPERDEPARTID from sys_department where SUPERDEPARTID='0'";

DBCPPool dbcpPool = new DBCPPool();
Connection conn = null;
Statement pstm = null;
ResultSet rs = null;
try {
conn = dbcpPool.getConnection();
pstm = dbcpPool.getStatement(conn);
rs = dbcpPool.getRowSet(pstm, sql);
while (rs.next()) {
SysDepartmentModel sysDepartmentModel = new SysDepartmentModel();
sysDepartmentModel.setDepartid(rs.getString("DEPARTID"));
sysDepartmentModel.setDepartname(rs.getString("DEPARTNAME"));
//sysDepartmentModel.setSuperdepartid(rs.getString("SUPERDEPARTID"));
sysdepartmentList.add(sysDepartmentModel);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbcpPool.closeConn(conn, pstm, null, null);
} catch (Exception e) {
logger.info(e.getMessage());
}
}
return sysdepartmentList;
}

/**
* 获取上级部门编号为DEPARTID的子部门列表
*/
public List<SysDepartmentModel> getDeptCList(String pid) {
// 上级部门的集合
List<SysDepartmentModel> smallList = new ArrayList<SysDepartmentModel>();
String sql = "select DEPARTID,DEPARTNAME,SUPERDEPARTID from sys_department where SUPERDEPARTID='" + pid + "'";
DBCPPool dbcpPool = new DBCPPool();
Connection conn = null;
Statement pstm = null;
ResultSet rs = null;
try {
conn = dbcpPool.getConnection();
pstm = dbcpPool.getStatement(conn);
rs = dbcpPool.getRowSet(pstm, sql);
while (rs.next()) {
SysDepartmentModel smallModel = new SysDepartmentModel();
smallModel.setDepartid(rs.getString("DEPARTID"));
smallModel.setDepartname(rs.getString("DEPARTNAME"));
smallModel.setSuperdepartid(rs.getString("SUPERDEPARTID"));
smallList.add(smallModel);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbcpPool.closeConn(conn, pstm, null, null);
} catch (Exception e) {
logger.info(e.getMessage());
}
}
return smallList;

}

===================================================== 五、SysDepartmentModel.java部分代码 =====================================================
package com.moa.model;

import java.io.Serializable;

public class SysDepartmentModel implements Serializable {
/*
* 部门编号
*/
private String departid;
/*
* 部门名称
*/
private String departname;
/*
* 上级部门编号
*/
private String superdepartid;

public String getDepartid() {
return departid;
}
public void setDepartid(String departid) {
this.departid = departid;
}
public String getDepartname() {
return departname;
}
public void setDepartname(String departname) {
this.departname = departname;
}
public String getSuperdepartid() {
return superdepartid;
}
public void setSuperdepartid(String superdepartid) {
this.superdepartid = superdepartid;
}
public SysDepartmentModel(String departid, String departname, String superdepartid) {
super();
this.departid = departid;
this.departname = departname;
this.superdepartid = superdepartid;
}
public SysDepartmentModel() {
super();
}

}
===================================================== 六、User.java部分代码 =====================================================
package com.moa.model;

import java.io.Serializable;

public class User implements Serializable {
private String suboffice_id;
private String section_id;
public String getSuboffice_id() {
return suboffice_id;
}
public void setSuboffice_id(String suboffice_id) {
this.suboffice_id = suboffice_id;
}
public String getSection_id() {
return section_id;
}
public void setSection_id(String section_id) {
this.section_id = section_id;
}
public User(String suboffice_id, String section_id) {
super();
this.suboffice_id = suboffice_id;
this.section_id = section_id;
}
public User() {
super();
}
}
===================================================== 七、sys_department表 =====================================================
create table SYS_DEPARTMENT
(
DEPARTID VARCHAR(6) not null,--部门编号
DEPARTNAME VARCHAR(80),--部门名称
SUPERDEPARTID VARCHAR(6),--上级部门编号
);

insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('1', '父部门1', '0');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('2', '父部门2', '0');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('3', '父部门2', '0');

insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('11', '子部门11', '1');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('12', '子部门12', '1');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('13', '子部门13', '1');

insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('21', '子部门21', '2');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('22', '子部门22', '2');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('23', '子部门23', '2');

insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('31', '子部门31', '3');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('32', '子部门32', '3');
insert into sys_department (DEPARTID, DEPARTNAME, SUPERDEPARTID)
values ('33', '子部门33', '3');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值