EasyUI(DataGrid修改删除)
生活随笔
收集整理的這篇文章主要介紹了
EasyUI(DataGrid修改删除)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
一、項目結構
二、前臺代碼
三、后臺代碼
四、運行效果:
一、項目結構
二、前臺代碼
editBook.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <div><form id="bookForm" method="post"> <input type="hidden" name="id" id="id"/><div style="margin: 15px;"> <label for="name">書名:</label> <input class="easyui-textbox" name="bookname" style="width:300px" data-options="required:true"> </div> <div style="margin: 15px;"> <label for="price">價格:</label> <input class="easyui-textbox" name="price" style="width:300px" data-options="required:true"> </div><div style="margin: 15px;"> <label for="booktype">類型:</label> <input class="easyui-textbox" name="booktype" style="width:300px" data-options="required:true"> </div> </form> </div>bookList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <%@ include file="../../common/head.jsp"%> <title>Insert title here</title> <script> $(function(){$('#bookTable').datagrid({ url: ctx+'/BookServlet', pagination:true,singleSelect:true,queryParams: {"bookName": $("#bookName").val()},columns:[[ {field:'id',title:'書本ID',width:100}, {field:'bookname',title:'名稱',width:100}, {field:'price',title:'價格',width:100,align:'right'},{field:'booktype',title:'類型',width:100,align:'right'}]],toolbar: '#bookTableToolbar'});$("#bookQry").click(function() {qryBook();});qryBook();function qryBook() {$('#bookTable').datagrid("load", {"bookName": $("#bookName").val()})};$("#addBookBtn").click(function() {openDialog();});$("#editBootBtn").click(function() {let row = $("#bookTable").datagrid("getSelected");openDialog(row);})$("#delBootBtn").click(function() {let row = $("#bookTable").datagrid("getSelected");if(!row) {$.messager.alert('消息','請選擇要刪除的記錄');return;}$.ajax({url: ctx + '/bookDelServlet',type: 'post',data:{id: row.id},dataType: 'json',success: function(resp) {if(resp.success) {$.messager.alert('消息','操作成功');qryBook();}else{$.messager.alert('消息','操作不成功');}}})})function openDialog(row) {let title = "增加書本信息";let action = "/bookAddServlet"if(row) {title = "修改書本信息";action = "/bookUpdateServlet"}$('#bookDiglog').dialog({ title: title, width: 400, height: 250, closed: false, cache: false, href: 'editBook.jsp', modal: true,buttons:[{text:'保存',handler:function(){$.ajax({url:ctx + action,data: $("#bookForm").serialize(),type: 'post',dataType: 'JSON',success: function(resp) {if(resp.success) {$.messager.alert('消息','操作成功');$('#bookDiglog').dialog('close');qryBook();} else {$.messager.alert('警告','操作失敗'); }}});}},{text:'關閉',handler:function(){$('#bookDiglog').dialog('close');}}],onLoad: function() {if(row) {$("#bookForm").form("reset");$("#bookForm").form("load", row);}}});} }); </script> </head> <body><!-- 查詢條件 --><div style="margin-top: 15px; margin-left:10px;"><input class="easyui-textbox" id="bookName" style="width:300px"><a id="bookQry" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查詢</a> </div> <div id="p" class="easyui-panel" style="padding:10px" data-options="fit:true, border:false"><table id="bookTable" class="easyui-datagrid" style="width:100%;height:90%;"> </table></div><!-- 列表上方的工具條 --><div id="bookTableToolbar" style="text-align: right;"><a href="#" id="addBookBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"/a><a href="#" id="editBootBtn" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a><a href="#" id="delBootBtn" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"/a></div><!-- 給彈出窗口定義一個容器,并默認為隱藏,在點擊后再顯示 --><div id="bookDiglog" style="display:none;"></div></body> </html>?
三、后臺代碼
BookDao:
package com.zking.demo.dao;import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List;import com.zking.demo.model.Book; import com.zking.demo.util.DBHelper;public class BookDao implements IBookDao {@Overridepublic List<Book> getBooks(String name, int pageIndex, int pageSize) {Connection con = null;PreparedStatement ps = null;ResultSet rs = null;List<Book> list = new ArrayList<>();try {String sql = "select id,bookname,price,booktype,rownum as rid from t_book";if(name != null && !"".equals(name)) {sql += " where bookname like ?";}sql = "select * from (" + sql + ")b where b.rid between ? and ?";con = DBHelper.getsCon();ps = con.prepareStatement(sql);int start =(pageIndex-1)*pageSize+1;int end = pageIndex*pageSize;if(name != null && !"".equals(name)) {ps.setString(1, "%"+name+"%");ps.setInt(2, start);ps.setInt(3, end);} else {ps.setInt(1, start);ps.setInt(2, end);}rs = ps.executeQuery();while(rs.next()) {Book m = new Book();m.setId(rs.getInt("id"));m.setBookname(rs.getString("bookname"));m.setPrice(rs.getBigDecimal("price"));m.setBooktype(rs.getString("booktype"));list.add(m);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.Close(con, ps, rs);}return list;}@Overridepublic int getTotalPage() {Connection con = null;PreparedStatement ps = null;ResultSet rs = null;int n = 0;try {con= DBHelper.getsCon();String sql = "select count(*) from t_book";ps = con.prepareStatement(sql);rs= ps.executeQuery();if(rs.next()) {n = rs.getInt(1);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.Close(con, ps, rs);}return n;}@Overridepublic void addBook(Book book) {Connection con = null;PreparedStatement ps = null;String sql = "insert into t_book value (id,bookname,price, booktype) "+ "select max(id)+1,'"+ book.getBookname()+"','" + book.getPrice()+"','" + book.getBooktype()+"' from t_book";try {con = DBHelper.getsCon();ps = con.prepareStatement(sql);ps.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {DBHelper.Close(con, ps, null);}}@Overridepublic void updateBook(Book book) {Connection con = null;PreparedStatement ps = null;String sql = "update t_book set bookname=?, price=?,booktype=? where id=?" ;try {con = DBHelper.getsCon();ps = con.prepareStatement(sql);ps.setString(1, book.getBookname());ps.setBigDecimal(2, book.getPrice());ps.setString(3, book.getBooktype());ps.setInt(4, book.getId());ps.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {DBHelper.Close(con, ps,null);}}@Overridepublic int delBook(int id) {Connection con = null;PreparedStatement ps = null;int n = 0;String sql = "delete t_book where id=?" ;try {con = DBHelper.getsCon();ps = con.prepareStatement(sql);ps.setInt(1, id);n = ps.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {DBHelper.Close(con, ps,null);}return n;}public static void main(String[] args) {BookDao dao = new BookDao();List<Book> books = dao.getBooks("貨", 1, 5);books.forEach(t -> System.out.println(t));}}BookService:
package com.zking.demo.service;import java.util.List;import com.zking.demo.dao.BookDao; import com.zking.demo.dao.IBookDao; import com.zking.demo.model.Book;public class BookService implements IBookService {private IBookDao dao = new BookDao();@Overridepublic List<Book> getBooks(String name, int pageIndex, int pageSize) {return dao.getBooks(name, pageIndex, pageSize);}@Overridepublic int getTotalPage() {return dao.getTotalPage();}@Overridepublic void addBook(Book book) {dao.addBook(book);}@Overridepublic void updateBook(Book book) {dao.updateBook(book);}@Overridepublic int delBook(int id) {// TODO Auto-generated method stubreturn dao.delBook(id);}}?
BookUpdateServlet:
package com.zking.demo.servlet;import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map;import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.alibaba.fastjson.JSON; import com.zking.demo.model.Book; import com.zking.demo.service.BookService; import com.zking.demo.service.IBookService;@WebServlet("/bookUpdateServlet") public class BookUpdateServlet extends HttpServlet {private IBookService service = new BookService();public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {doPost(req, resp);}public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {req.setCharacterEncoding("utf-8");resp.setContentType("application/json; charset=utf-8"); Map<String,Object> map = new HashMap<>();try {Book book = new Book();book.setId(Integer.parseInt(req.getParameter("id")));book.setBookname(req.getParameter("bookname"));book.setPrice(BigDecimal.valueOf(Double.parseDouble(req.getParameter("price"))));book.setBooktype(req.getParameter("booktype"));service.updateBook(book);map.put("success", true);} catch (Exception e) {e.printStackTrace();map.put("success", false);}PrintWriter out = resp.getWriter();out.write(JSON.toJSONString(map));out.flush();out.close();}}?
BookDelServlet:
package com.zking.demo.servlet;import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map;import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.alibaba.fastjson.JSON; import com.zking.demo.service.BookService; import com.zking.demo.service.IBookService;@WebServlet("/bookDelServlet") public class BookDelServlet extends HttpServlet {private IBookService service = new BookService();public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {doPost(req, resp);}public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {req.setCharacterEncoding("utf-8");resp.setContentType("application/json; charset=utf-8"); Map<String,Object> map = new HashMap<>();int id = Integer.parseInt(req.getParameter("id"));int n = service.delBook(id);if(n > 0) {map.put("success", true);}else {map.put("success", false);}PrintWriter out = resp.getWriter();out.write(JSON.toJSONString(map));out.flush();out.close();}}?
四、運行效果:
?
總結
以上是生活随笔為你收集整理的EasyUI(DataGrid修改删除)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现 Vue 服务端渲染(Vue SSR
- 下一篇: 系统软件调试的培训大纲