easyui(datagrid数据新增)

一、增加的dao方法

public int addBook(Book b) {
		int n=0;
		try {
			con=DBHelper.getCon();
			String sql="insert into tb_book(bid,bname,bprice,btype) select nvl(max(bid),0)+1,?,?,? from tb_book";
			ps=con.prepareStatement(sql);
			ps.setString(1, b.getBname());
			ps.setDouble(2, b.getBprice());
			ps.setString(3, b.getBtype());
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps, rs);
		}
		return n;
	}

二、增加的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("/addBook.do")
public class addBookServlet 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();
		
		//接收前台传递过来的参数 bname  bprice  btype
		String bname=request.getParameter("bname");//书籍名称
		String a=request.getParameter("bprice");//书籍价格
		String btype=request.getParameter("btype");//书籍类型
		double bprice=0;
		if(a!=null) {//将String-->double
			bprice=Double.parseDouble(a);
		}
		//servlet调用biz
		IBookBiz ibb=new BookBiz();
		//实例化一个书籍对象
		Book b=new Book(bname, bprice, btype);
		//调用增加方法
		int n=ibb.addBook(b);
		String str="error";
		if(n>0) {//说明增加成功
			str="success";
		}
		//输送到页面
		out.write(str);
		out.flush();
		out.close();
	}

}

三、增加的界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="myForm" method="post">   
	    <div style="margin-top: 20px;">   
	        <label for="name">书籍名称:</label>   
	        <input class="easyui-textbox" type="text" name="bname" data-options="required:true" style="width:200px"/>   
	    </div>   
	    <div style="margin-top: 20px;">   
	        <label for="email">书籍价格:</label>   
	        <input class="easyui-textbox" type="text" name="bprice" data-options="required:true" style="width:200px" />   
	    </div>  
	    <div style="margin-top: 20px;">   
	        <label for="email">书籍类型:</label>   
	        <input class="easyui-textbox" type="text" name="btype" data-options="required:true" style="width:200px" />   
	    </div> 
	</form>
</body>
</html><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form id="myForm" method="post">   
	    <div style="margin-top: 20px;">   
	        <label for="name">书籍名称:</label>   
	        <input class="easyui-textbox" type="text" name="bname" data-options="required:true" style="width:200px"/>   
	    </div>   
	    <div style="margin-top: 20px;">   
	        <label for="email">书籍价格:</label>   
	        <input class="easyui-textbox" type="text" name="bprice" data-options="required:true" style="width:200px" />   
	    </div>  
	    <div style="margin-top: 20px;">   
	        <label for="email">书籍类型:</label>   
	        <input class="easyui-textbox" type="text" name="btype" data-options="required:true" style="width:200px" />   
	    </div> 
	</form>
</body>
</html>

四、给+按钮添加点击事件

//给+号添加点击事件
		$("#addBtn").click(function(){
			//显示一个对话框
			$('#myDialog').dialog({    
			    title: 'My Dialog', //标题  
			    width: 400, //宽度
			    height: 300,//高度    
			    closed: false,//是否能关闭    
			    cache: false,  //是否有缓存  
			    href: 'editBook.jsp',//加载的页面    
			    modal: true ,//是否出现模态框效果
			    buttons:[{
					text:'提交',
					handler:function(){
						//向后台发送ajax请求
						$.ajax({
							url:ctx+"/addBook.do",//请求地址 servlet
							data:$("#myForm").serialize(),//请求参数
							type:"post",//请求方式 默认为get
							dataType:"text",//预期可能返回的数据类型
							success:function(data){//成功的回调函数
								if(data=="success"){
									//成功
									$.messager.alert('消息','提交成功');    
									//关闭对话框
									$("#myDialog").dialog("close");
									//调用刷新方法
									myShow();
								}
								else{
									//失败
									$.messager.alert('警告','提交失败');
								}
							},
							error:function(){//失败的回调函数
								alert("有误");
							}
						});
						
						/* $.post(ctx+"/addBook.do",$("#myForm").serialize(),function(){
							alert(data);
						}) */
					}
				},{
					text:'关闭',
					handler:function(){
						//关闭对话框
						$('#myDialog').dialog("close");
					}
				}]

			});
		})

界面展示:

myShow方法:

function myShow(){
		//加载新的数据
		$('#myTable').datagrid('load',{
			bname:$('#str').val(),//传递参数到后台
			btype:$('#xl').val()
		});
	}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值