1.批量删除
有个bug,只能在前端删除,数据库中删不掉
Admincontroller中加
@RequestMapping("/delCourse")
public String batchDeletes(HttpServletRequest request, HttpServletResponse response) {
String items = request.getParameter("delitems");
System.out.println(items);
String[] strs = items.split(",");
for (int i = 0; i < strs.length; i++) {
try {
int id = Integer.parseInt(strs[i]);
courseService.removeById(id);
// persistService.delStudentById(a);//这行代码debug了应该就能真正删掉数据库数据了
} catch (Exception e) {
}
}
return "redirect:/admin/showCourse";
}
showcourse.jsp中加
<table class="table table-bordered">
<thead>
<tr>
<th>选择</th> //
<th>课程号</th>
<th>课程名称</th>
<th>授课老师编号</th>
<th>上课时间</th>
<th>上课地点</th>
<th>周数</th>
<th>课程类型</th>
<th>学分</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${courseList}" var="item">
<tr>
<td><input type="checkbox" id="subcheck" name="subcheck" value="${item.courseid}" /></td> //
<td>${
item.courseid}</td>
<td>${
item.coursename}</td>
<td>${
item.teacherid}</td>
<td>${
item.coursetime}</td>
<td>${
item.classroom}</td>
<td>${
item.courseweek}</td>
<td>${
item.coursetype}</td>
<td>${
item.score}</td>
<td>
<button class="btn btn-default btn-xs btn-info" onClick="location.href='/admin/editCourse?id=${item.courseid}'">修改</button>
<button class="btn btn-default btn-xs btn-danger btn-primary" onClick="location.href='/admin/removeCourse?id=${item.courseid}'">删除</button>
<!--弹出框-->
</td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="button" class="btn btn-default btn-xs btn-danger btn-primary" onclick="batchDeletes()" value="删除"></input> //
<script type="text/javascript"> //
function batchDeletes(){
//判断至少写了一项
var checkedNum = $("input[name='subcheck']:checked").length;
if(checkedNum==0){
alert("请至少选择一项!");
return false;
}
if(confirm("确定删除所选项目?")){
var checkedList = new Array();
$("input[name='subcheck']:checked").each(function(){
checkedList.push($(this).val());
});
$.ajax({
type:"POST",
url:"delCourse",
data:{
"delitems":checkedList.toString()},
datatype:"html",
success:function(data){
$("[name='checkbox']:checkbox").attr("checked",false);
location.reload();//页面刷新
},
error:function(data){
alert('删除失败!');
}
});
}
}
</script>
2.搜索功能
showcourse.jsp
<form class="bs-example bs-example-form col-md-5" role="form" style="margin: 20px 0 10px 0;" action="/admin/selectCourse2" id="form2" method="post">
<div class="input-group">
<input type="number" class="form-control" placeholder="授课教师编号" name="findByTeacherID">
<span class="input-group-addon btn" onclick="document.getElementById('form2').submit" id="sub2">搜索</span>
</div>
</form>
//在最后面加
$("#sub2").click(function () {
$("#form2").submit();
});
admincontroller中
@RequestMapping(value = "selectCourse2", method = {
RequestMethod.POST})
private String selectCourse2(Integer findByTeacherID, Model model) throws Exception {
List<CourseCustom> list = courseService.findByTeacherID(findByTeacherID);
model.addAttribute("courseList", list);
return "admin/showCourse";
}