增加数据模块:
//信息添加按钮事件
$("#add").click(function () {
table = $("#addTable");
$("#addTable").form("clear");//清空表单数据
$("#addDialog").dialog("open");//打开添加窗口
});
//设置添加学生信息窗口
$("#addDialog").dialog({
title: "添加学生信息窗口",
...
buttons: [
{
text: '添加',
plain: true,
iconCls: 'icon-add',
handler: function () {
var validate = $("#addForm").form("validate");
if (!validate) {
$.messager.alert("消息提醒", "请检查你输入的数据哟!", "warning");
} else {
var data = $("#addForm").serialize();//序列化表单信息
$.ajax({
type: "post",
url: "addStudent?t" + new Date().getTime(), // 发送ajax请求
data: data,
dataType: 'json',
success: function (data) {
if (data.success) {
$("#addDialog").dialog("close"); //关闭窗口
$('#dataList').datagrid("reload");//重新刷新页面数据
$.messager.alert("消息提醒", "添加成功啦!", "info");
} else {
$.messager.alert("消息提醒", data.msg, "warning");
}
}
});
}
}
},
{
text: '重置',
plain: true,
iconCls: 'icon-reload',
handler: function () {
$("#add_sno").textbox('setValue', "");
$("#add_name").textbox('setValue', "");
$("#add_gender").textbox('setValue', "男");
$("#add_password").textbox('setValue', "");
$("#add_email").textbox('setValue', "");
$("#add_telephone").textbox('setValue', "");
$("#add_address").textbox('setValue', "");
$("#add_introducation").textbox('setValue', "");
}
}
]
});
url: "addStudent?t" + new Date().getTime() 转到Controller
// StudentController.java
@PostMapping("/addStudent")
@ResponseBody
public Map<String, Object> addStudent(Student student) {
//判断学号是否已存在
if (studentService.fingBySno(student) != null) {
result.put("success", false);
result.put("msg", "该学号已经存在! 请修改后重试!");
return result;
}
//添加学生信息
if (studentService.insert(student) > 0) {
result.put("success", true);
} else {
result.put("success", false);
result.put("msg", "添加失败! (ಥ_ಥ)服务器端发生异常!");
}
return result;
}
返回响应后的回调函数:
success: function (data) {
if (data.success) {
$("#addDialog").dialog("close"); //关闭窗口
$('#dataList').datagrid("reload");//重新刷新页面数据
$.messager.alert("消息提醒", "添加成功啦!", "info");
} else {
$.messager.alert("消息提醒", data.msg, "warning");
}
}
修改数据模块:
//信息修改按钮事件
$("#edit").click(function () {
table = $("#editTable");
var selectRows = $("#dataList").datagrid("getSelections");
if (selectRows.length !== 1) {
$.messager.alert("消息提醒", "请单条选择想要修改的数据哟!", "warning");
} else {
$("#editDialog").dialog("open");
}
});
//设置编辑学生信息窗口
$("#editDialog").dialog({
title: "修改学生信息窗口",
...
buttons: [
{
text: '提交',
plain: true,
iconCls: 'icon-edit',
handler: function () {
var validate = $("#editForm").form("validate");
if (!validate) {
$.messager.alert("消息提醒", "请检查你输入的数据哟!", "warning");
} else {
var data = $("#editForm").serialize();//序列化表单信息
$.ajax({
type: "post",
url: "editStudent?t=" + new Date().getTime(), // 发送ajax请求
data: data,
dataType: 'json',
success: function (data) {
// 关闭窗口
// 重新刷新页面数据
// 用户提示
}
});
}
}
},
{
text: '重置',
plain: true,
iconCls: 'icon-reload',
handler: function () {
// 同增加模块
}
}
],
//打开窗口前先初始化表单数据(表单回显)
onBeforeOpen: function () {
var selectRow = $("#dataList").datagrid("getSelected");
$("#edit_id").val(selectRow.id);//初始化id值,需根据id更新学生信息
$("#edit_sno").textbox('setValue', selectRow.sno);
$("#edit_name").textbox('setValue', selectRow.name);
$("#edit_gender").textbox('setValue', selectRow.gender);
$("#edit_password").textbox('setValue', selectRow.password);
$("#edit_email").textbox('setValue', selectRow.email);
$("#edit_telephone").textbox('setValue', selectRow.telephone);
$("#edit_address").textbox('setValue', selectRow.address);
$("#edit_introducation").textbox('setValue', selectRow.introducation);
//通过获取头像路径来显示该学生的头像
$("#edit-portrait").attr('src', selectRow.portrait_path);
//初始化头像路径(已优化:在执行SQL语句时通过判断头像路径是否为空,为空则代表用户并未修改头像)
//$("#edit_portrait-path").val(selectRow.portrait_path);
}
});
url: "editStudent?t=" + new Date().getTime(), 转到Controller
@PostMapping("/editStudent")
@ResponseBody
public Map<String, Object> editStudent(Student student) {
if (studentService.update(student) > 0) {
result.put("success", true);
} else {
result.put("success", false);
result.put("msg", "添加失败! (ಥ_ಥ)服务器端发生异常!");
}
return result;
}
返回响应的回调函数:
success: function (data) {
if (data.success) {
//关闭窗口
$("#editDialog").dialog("close");
//重新刷新页面数据
$('#dataList').datagrid("reload");
$('#dataList').datagrid("uncheckAll");
//用户提示
$.messager.alert("消息提醒", "修改成功啦!", "info");
} else {
$.messager.alert("消息提醒", data.msg, "warning");
}
}
删除操作类似