SSM框架的(CRUD)_新增_Ajax显示部门信息15
2020-01-08 本文已影响0人
念念碎平安夜
一、点击新增按钮时,需要发送ajax请求,查出部门信息,显示在下拉列表中
$("#emp_add_modal_btn").click(function(){
//发送ajax请求,查出部门信息,显示在下拉列表中
getDepts();
//弹出模态框
$("#empAddModal").modal({
backdrop:"static"
});
});
二、编写方法
//查出所有的部门信息并显示在下拉列表中
function getDepts(){
$.ajax({
url:"${APP_PATH}/depts",
type:"GET",
success:function(result){
console.log(result)
}
})
};
三、创建控制层代码DepartmentController
package com.christmaseve.crud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.christmaseve.crud.bean.Department;
import com.christmaseve.crud.bean.Msg;
import com.christmaseve.crud.service.DepartmentService;
/**
* 处理和部门有关的请求
* @author zhangcheng
*
*/
@Controller
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
/**
* 返回所有的部门信息
*/
@RequestMapping("/depts")
@ResponseBody
public Msg getDepts() {
//
List<Department> list = departmentService.getDepts();
return Msg.success().add("depts", list);
}
}
四、创建逻辑层代码DepartmentService
package com.christmaseve.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.christmaseve.crud.bean.Department;
import com.christmaseve.crud.dao.DepartmentMapper;
@Service
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
public List<Department> getDepts() {
// TODO Auto-generated method stub
List<Department> list = departmentMapper.selectByExample(null);
return list;
}
}
五、请求成功回调函数里添加
$.each(result.extend.depts,function(){
var optionEle = $("<option></option>").append(this.deptName).attr("value",this.deptId);
optionEle.appendTo("#empAddModal select");
});