web 开发问题记录
1.jquery获取table表格指定行内容数据
$("a[name='checkOriginal']").click(function () {
var parent = $(this).parent().parent().find("td");
var moduleEnum = parent.eq(7).text();
if(moduleEnum==""){
}
alert(moduleEnmu);
});
2.使用ajax发起delete请求时,直接把需要删除的iD带在访问的URL后面(省事).
$.ajax({
type:"delete",
url:"/um/deleteUser?id="+data.id,
success:function (data){
if (data.status == 200){
layer.alert('删除成功', {icon: 1});
}else{
layer.alert('删除失败', {icon: 1});
}
}
})
3.多行删除,难点
前端:需要拿到选中的行数,通过ajax发送delete请求给到后端。
var ids = [];
//把获取到的值传到定义数组里面
//getSelections easyUI中选中多条数据的方法
var selectRows = $("#dataList").datagrid("getSelections");
for (let i = 0; i < selectRows.length; i++) {
ids.push(selectRows[i].courseId)
}
$.ajax({
type: "delete",
url: "/tm/courseList",
dataType: "json",
contentType:"application/json",
data: JSON.stringify(ids),//转成json字符串传给前端
success: function(msg){
if(msg.status == 200){
$.messager.alert("消息提醒","删除成功!","info");
//刷新表格
$("#dataList").datagrid("reload");
} else{
$.messager.alert("消息提醒","删除失败!","warning");
return;
}
}
});
后端:拿到前端给到后端的值,解包成为Integer数组。
//解析前端传来的json字符串
Object obj = new ObjectMapper().readValue(req.getInputStream(), Object.class);
System.out.println(obj);
ArrayList list = (ArrayList) obj;
RespMsg respMsg = null;
//新建一个int 数组保存前端传来的ID数据
Integer countRows[] = new Integer[list.size()];
for (int i = 0; i < list.size(); i++) {
countRows[i] = (int) list.get(i);
}
Integer integerResult = courseService.deleteCourseByGroup(countRows);
dao层:拿到数组遍历,逐行删除。
public Integer deleteCourseByGroup(Integer[] countRows) {
boolean deleteSuccess = false;
for (int i = 0; i < countRows.length; i++) {
if (countRows[i] != 0) {
System.out.println("数据库删除了编号为:" + countRows[i] + "的课程");
try {
//一直删
queryRunner.update("delete from course where courseid =?", countRows[i]);
deleteSuccess = true;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}else {
break;
}
}
if (deleteSuccess){
return 1;
}else {
return -1;
}
}