一、数据删除
数据删除思路:首先判断是否选中表格
var s=$('#myTab').datagrid('getSelected');
如果s不等于null,就说明选中,等于null就给予提示
if(s!=null){//说明选中行
}
else{
//提示要求选中
$.messager.alert('提示','请先选中你要删除的行数');
}
选中后就把ID输送到后台然后进行删除
//给删除添加点击事件
$("#removeBtn").click(function() {
//判断是否选中
var s=$('#myTab').datagrid('getSelected');
$.messager.confirm('确认','您确认想要删除记录吗?',function(r){
if (r){
if(s!=null){//说明选中行
//利用ajax先后台发送数据
$.post(ctx+"/DeleteBookServlet", {bid:s.bid}, function(data) {
if(data=="Yes"){
$.messager.alert('提示','删除成功');
myShow();
}
else{
$.messager.alert('提示','删除失败');
}
})
}
else{
//提示要求选中
$.messager.alert('提示','请先选中你要删除的行数');
}
}
});
})
DeleteBookServlet页面代码
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;
@WebServlet("/DeleteBookServlet")
public class DeleteBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//三个编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//拿到out
PrintWriter out = response.getWriter();
//获取前台传来的参数
String bid=request.getParameter("bid");//书籍编号
//调用biz层
IBookBiz ibb=new BookBiz();
int n = ibb.deleteBook(Integer.parseInt(bid));
String ss="no";
if(n>0) {
ss="Yes";
}
out.write(ss);
out.flush();
out.close();
}
}
二、修改
修改思路:首先选中行
//给修改添加点击事件
$("#deleteBtn").click(function(){
//判断是否选中
var s=$('#myTab').datagrid('getSelected');
if(s!=null){//说明选中行
openDialog(s);//打开对话框
}
else{
//提示要求选中
$.messager.alert('提示','请先选中你要修改的行数');
}
})
这个时候封装一个对话框openDialog
//打开对话框
function openDialog(s) {
alert(s);
var title="增加书籍";
var url="/addBook.do";
if(s!=undefined){//说明是修改
title="修改书籍";
url="/updateBook.do";
}
//显示一个对话框
$('#myDialog').dialog({
title: title,//标题
width: 400,//宽度
height: 300,//高度
closed: false,//是否能关闭
cache: false,//是否有缓存
href: 'addBook.jsp',//加载页面
modal: true,//是否出现模态框的效果
onLoad:function(){
if(s!=undefined){//说明是修改
$("#myFrom").form('reset');
$("#myFrom").form('load',s);
}
},
buttons:[{
text:'提交',
handler:function(){
//向后台发送ajax请求
$.ajax({
url:ctx+url,//请求路径
data:$("#myFrom").serialize(),//请求参数
type:"post",//请求方法
dataType:"text",//预期可能返回的数据类型
success:function(data){//成功的回掉函数
if(data=="Yes"){
//增加成功
$.messager.alert('消息','增加成功');
//关闭对话框
$('#myDialog').dialog("close");
//刷新数据
myShow();
}
else{
//增加失败
$.messager.alert('警告','增加失败');
//关闭对话框
$('#myDialog').dialog("close");
}
},
error:function(){//失败的回掉函数
$.messsager.alert('警告','有误');
}
});
}
},{
text:'关闭',
handler:function(){
//关闭对话框
$('#myDialog').dialog("close");
}
}]
});
}
然后我们编写servlet
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.entity.Book;
@WebServlet("/updateBook.do")
public class UpdateBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//三个编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//拿到out
PrintWriter out = response.getWriter();
//获取前台传来的参数
String bid=request.getParameter("bid");
String bnaem=request.getParameter("bnaem");//书籍名称
String a=request.getParameter("bprice");//书籍价格
String btype=request.getParameter("btype");//书籍类型
double bprice=0;
if(a!=null) {
bprice=Double.parseDouble(a);
}
//调用biz层
IBookBiz ibb=new BookBiz();
int n = ibb.updateBook(Integer.parseInt(bid), new Book(bnaem, bprice, btype));
String ss="no";
if(n>0) {
ss="Yes";
}
out.write(ss);
out.flush();
out.close();
}
}