ajax方式选择部门获取部门中所有成员

来源:互联网 发布:谈大数据的重要性 编辑:程序博客网 时间:2024/04/29 22:58

前端代码:

选择部门的前端代码:<s:select id="deptId" name="dept.id" list="deptList" onchange="changeManager()" listKey="id" listValue="deptName" headerKey="" headerValue="--请选择--"></s:select>选择部门人员的前端代码:<select name="userInfo.manager.id" id="managers"><option value="">--请选择--</option></select>

ajax代码:

function changeManager(){//获取选中的部门var deptId = $("#deptId>option:selected").val();//alert(deptId);$.ajax({url:'userAction_findManagers?deptId='+deptId,dataType:'text',type:'get',success:function(data){$("#managers")[0].length=1;//清空managers下拉列表框                      //使用jQuery获取对象都是数组对象,立面存放的是一个个js对象,可以通过下标的方式访问这些对象,取出来的就是js对象data=eval("("+data+")");//分割data,使之成为数组$(data).each(function(){$("#managers").append("<option value='"+this.id+"'>"+this.userName+"</option>");});}});};

后台:

传递json数据方式一:struts2-json-plugin方式:

第一步:action编写逻辑代码

public String findManagers() throws Exception{//deptId通过属性注入方式获取 List<User> list = userService.find("from User where dept.id=?", User.class, new String[]{deptId});// this.push();return "findManagers";

第二步:result结果集

<package name="sysadmin" namespace="/sysadmin" extends="json-default">//必须要继承json-default

<result name="findManagers" type="json"></result>//不用跳转任何链接,ajax自动跳回原界面,type类型必须为json格式

第三步:实体类

//这里注意如果存在关联实体必须加注解,而struts2-json方式的注解是加在get方法上,fastjson是加在属性上private Set<Role> roles = new HashSet<Role>();@JSON(serialize=false)public Set<Role> getRoles() {  return roles;}public void setRoles(Set<Role> roles) {  this.roles = roles;}

传递json数据方式二:fastjson方式:

第一步 :action功能代码

public String findManagers() throws Exception{ List<User> list = userService.find("from User where dept.id=?", User.class, new String[]{deptId});//这里注意不要导错了包。是fastjson的包 String jsonString = JSON.toJSONString(list);//下面三段代码很常用可以加入工具类 HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); response.getWriter().write(jsonString);//因为直接通过流写会到前端所以这里直接返回NONE  return NONE;
第二步:实体类

//这里注意如果存在关联实体必须加注解,而struts2-json方式的注解是加在get方法上,fastjson是加在属性上
@JSONField(serialize=false)private
Set<Role> roles = new HashSet<Role>();

public Set<Role> getRoles() {
return roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}

传递json数据方式三:后台拼接方式:

第一步:action后台功能代码

public String findManagers() throws Exception {//根据部门id查找部门的领导信息//List<User> userList = userService.find("from User where dept.id = ? and userInfo.degree != 4", User.class, new String[]{deptId});List<User> userList = userService.find("from User where dept.id = ?", User.class, new String[]{deptId});//拼接返回的串信息String result = "";for(User u:userList){result+="<option value='"+u.getId()+"'>"+u.getUserName()+"</option>";}//手动返回HttpServletResponse response = ServletActionContext.getResponse();response.setCharacterEncoding("utf-8");response.getWriter().write(arges);//返回值return NONE;}

第二步:前端代码:

$.ajax({url:'userAction_findManagers?deptId='+deptId,dataType:'text',type:'get',success:function(data){//清空managers下拉列表框$("#managers")[0].length=1;$("#managers").append(data);}});





 
原创粉丝点击