EasyUI项目之书籍管理(CRUD)
生活随笔
收集整理的這篇文章主要介紹了
EasyUI项目之书籍管理(CRUD)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在上節(jié)課的基礎(chǔ)上對(duì)書籍管理增加CRUD操作
目標(biāo)效果:
目標(biāo):
1,點(diǎn)擊新增,類型將會(huì)在下拉框中顯示,增加成功后,該書會(huì)顯示到未上架界面中。
2,未上架,已上架(上架,下架書籍是改變書籍的狀態(tài))。
3,已下架,查詢狀態(tài)為下架的書籍。
4,修改,在未上架中可選中書籍進(jìn)行修改。
打開選項(xiàng)卡 (在上節(jié)課基礎(chǔ)上完善選項(xiàng)卡功能)
$(function() {$("#bookMenus").tree({url:$("#ctx").val()+"/permission.action?methodName=tree",onClick:function(node){//判斷有無重復(fù)選項(xiàng)卡var exists=$('#bookTabs').tabs('exists',node.text);if(exists){//存在則選中$('#bookTabs').tabs('select',node.text);}else{//不存在則打開新的選項(xiàng)卡$('#bookTabs').tabs('add',{ title:node.text, //node.attributes.self.menuURL決對(duì)路徑content:'<iframe width="100%" height="100%" src="'+$("#ctx").val()+node.attributes.self.url+'"></iframe>', closable:true, tools:[{ iconCls:'icon-mini-refresh', handler:function(){ alert('refresh'); } }] });}}}); })一,增加
1.1 ComboBox(利用ComboBox控件制作下拉列表框)
實(shí)體類
CategoryDao
CategoryAction
package com.zking.web;import java.util.List;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.zking.dao.CategoryDao; import com.zking.entity.Category; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.ResponseUtil;public class CategoryAction extends ActionSupport implements ModelDriver<Category> {private Category category = new Category();private CategoryDao categoryDao = new CategoryDao();public Category getModel() {return category;}// 類別下拉框public String combobox(HttpServletRequest req, HttpServletResponse resp) throws Exception { // 獲取下拉框值List<Category> list = categoryDao.list(category, null);ResponseUtil.writeJson(resp, list);return null;}}配置xml文件
<action path="/category" type="com.zking.web.CategoryAction"></action>js代碼
$(function () {$('#cid').combobox({url:'${pageContext.request.contextPath}/category.action?methodName=combobox',valueField:'id',textField:'name'})});//表單提交增加按鈕function submitForm() {}function clearForm() {}界面代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>書籍新增</title><link rel="stylesheet" type="text/css"href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css"><script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script><script type="text/javascript"src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script><script src="${pageContext.request.contextPath}/static/js/main.js"></script> </head> <body> <div style="margin:20px 0;"></div> <div class="easyui-panel" title="已下架書籍" style="width:100%;padding:30px 60px;"><form id="ff" action="" method="post"><div style="margin-bottom:20px"><input class="easyui-textbox" name="name" style="width:100%" data-options="label:'書名:',required:true"></div><div style="margin-bottom:20px"><input id="cid" name="cid" value="" label="類別" ><%--<select class="easyui-combobox" name="cid" label="類別" style="width:100%">--%><%--<option value="1">文藝</option>--%><%--<option value="2">小說</option>--%><%--<option value="3">青春</option>--%><%--</select>--%></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="price" style="width:100%"data-options="label:'價(jià)格:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="publishing" style="width:100%"data-options="label:'出版社:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="description" style="width:100%;height:60px"data-options="label:'簡(jiǎn)介:',required:true"></div><%--默認(rèn)未上架--%><input type="hidden" name="state" value="1"><%--默認(rèn)起始銷量為0--%><input type="hidden" name="sales" value="0"></form><div style="text-align:center;padding:5px 0"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">提交</a><a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">重置</a></div> </div> <script>$(function () {$('#cid').combobox({url:'${pageContext.request.contextPath}/category.action?methodName=combobox',valueField:'id',textField:'name'})});//表單提交增加按鈕function submitForm() {}function clearForm() {}</script> </body> </html>效果展示:
2.2 增加
Book
// 增加public void add( Book book) throws Exception {String sql = "insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)";//設(shè)置拼音book.setPinyin(PinYinUtil.getAllPingYin(book.getName()));//設(shè)置時(shí)間book.setDeployTime(new Date());super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});}BookAction
//增加public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {try {bookDao.add(book);ResponseUtil.writeJson(resp, 1);} catch (Exception e) {e.printStackTrace();ResponseUtil.writeJson(resp, 0);}}拼音工具類:
package com.zking.util;import java.util.regex.Pattern;import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;/*** 拼音工具類,能將漢字轉(zhuǎn)換成拼音的首字母*/ public class PinYinUtil {// 名字長(zhǎng)度private static int NAME_LENGTH = 3;/*** 將首個(gè)漢字轉(zhuǎn)換為全拼* 其他是漢字首字母* @param src* @return*/public static String getPingYin(String src) {char[] name = src.toCharArray();String[] newName = new String[name.length];HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);String account = "";int length = name.length;try {// 名字大于等于3個(gè)字的時(shí)候,姓取全稱,名取首字母。if(length>=NAME_LENGTH){for (int i = 0; i < length; i++) {// 截取姓if(i==0){// 判斷是否為漢字字符if (Character.toString(name[i]).matches("[\\u4E00-\\u9FA5]+")) {newName = PinyinHelper.toHanyuPinyinStringArray(name[i], pyFormat);account += newName[0];} elseaccount += Character.toString(name[i]);}else{account += getPinYinHeadChar(Character.toString(name[i]));}}}else{// 只有2個(gè)字的名字,賬號(hào)是名字的拼音全稱for (int i = 0; i < length; i++) {// 判斷是否為漢字字符if (Character.toString(name[i]).matches("[\\u4E00-\\u9FA5]+")) {newName = PinyinHelper.toHanyuPinyinStringArray(name[i], pyFormat);account += newName[0];} elseaccount += Character.toString(name[i]);}}return account;} catch (BadHanyuPinyinOutputFormatCombination e1) {e1.printStackTrace();}return account;}/*** 全部漢字轉(zhuǎn)換成拼音* @param src* @return*/public static String getAllPingYin(String src) {StringBuffer sb = new StringBuffer();String [] arr = src.split("");for (String s : arr) {sb.append(PinYinUtil.getPingYin(s));}return sb.toString();}/*** 返回中文的首字母* @param str* @return*/public static String getPinYinHeadChar(String str) {String convert = "";for (int j = 0; j < str.length(); j++) {char word = str.charAt(j);String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);if (pinyinArray != null) {convert += pinyinArray[0].charAt(0);} else {convert += word;}}return convert;}public static void main(String[] args) {String cn = "保存并插入數(shù)據(jù)庫";System.out.println(PinYinUtil.getAllPingYin(cn));System.out.println(PinYinUtil.getPingYin(cn));System.out.println(PinYinUtil.getPinYinHeadChar(cn));} }配置xml文件
<action path="/book" type="com.zking.web.BookAction"><forward name="findBook" path="/fg/findBook.jsp" redirect="false"> </forward></action>js代碼
//表單提交增加按鈕function submitForm() {$('#ff').form('submit',{url:'${pageContext.request.contextPath}/book.action?methodName=add',success:function (data) {if(data == 1){$('#ff').form('clear');}}});}function clearForm() {$('#ff').form('clear');}效果展示:
二,上架與下架
Bookdao
//上架與下架public void editStatus(Book book) throws Exception {String sql = "update t_easyui_book set state=? where id=?";super.executeUpdate(sql, book, new String[] {"state","id"});}BookAction
//上架與下架public void editStatus(HttpServletRequest req, HttpServletResponse resp) throws Exception {try {bookDao.editStatus(book);ResponseUtil.writeJson(resp, 1);} catch (Exception e) {e.printStackTrace();ResponseUtil.writeJson(resp, 0);}}上架js代碼
function shangjia() {$.messager.confirm('確認(rèn)','您確認(rèn)想要上架此書籍嗎?',function(r){if (r){var row = $('#dg').datagrid('getSelected');if (row){$.ajax({url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=2&id=' + row.id,success:function (data) {$('#dg').datagrid('reload');}})} }});}下架js代碼
function xiajia() {$.messager.confirm('確認(rèn)','您確認(rèn)想要下架此書籍嗎?',function(r){if (r){var row = $('#dg').datagrid('getSelected');if (row){$.ajax({url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=3&id=' + row.id,success:function (data) {$('#dg').datagrid('reload');}})}}});}上架界面代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>未上架書籍</title><link rel="stylesheet" type="text/css"href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css"><script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script><script type="text/javascript"src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script><script src="${pageContext.request.contextPath}/static/js/main.js"></script> </head> <body><%--未上架書籍--%> <table id="dg" style="style=" width:400px;height:200px; "></table><div id="tb"><input class="easyui-textbox" id="name" name="name" style="width:20%;padding-left: 10px" data-options="label:'書名:',required:true"><a id="btn-search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a> </div><!-- 彈出框提交表單所用 --> <div id="dd" class="easyui-dialog" title="編輯窗體" style="width:600px;height:450px;"data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'"><form id="ff" action="${pageContext.request.contextPath}/book.action?methodName=edit" method="post"><div style="margin-bottom:20px"><input class="easyui-textbox" name="name" style="width:100%" data-options="label:'書名:',required:true"></div><div style="margin-bottom:20px"><input id="cid" name="cid" value="" label="類別" ><%--<select class="easyui-combobox" name="cid" label="類別" style="width:100%">--%><%--<option value="1">文藝</option>--%><%--<option value="2">小說</option>--%><%--<option value="3">青春</option>--%><%--</select>--%></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="price" style="width:100%"data-options="label:'價(jià)格:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="publishing" style="width:100%"data-options="label:'出版社:',required:true"></div><div style="margin-bottom:20px"><input class="easyui-textbox" name="description" style="width:100%;height:60px"data-options="label:'簡(jiǎn)介:',required:true"></div><%--默認(rèn)未上架--%><input type="hidden" name="state" value=""><%--默認(rèn)起始銷量為0--%><input type="hidden" name="sales" value=""><input type="hidden" name="id" value=""><input type="hidden" name="image" value=""></form><div style="text-align:center;padding:5px 0"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">提交</a><a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">重置</a></div></div><!-- 圖片上傳 --> <div id="dd2" class="easyui-dialog" title="書籍圖標(biāo)上傳" style="width:600px;height:450px;"data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'"><form id="ff2" action="" method="post" enctype="multipart/form-data"><div style="margin-bottom:20px"><input type="file" name="file"></div></form><div style="text-align:center;padding:5px 0"><a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm2()" style="width:80px">提交</a><a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">重置</a></div></div></body> <script>function shangjia() {$.messager.confirm('確認(rèn)','您確認(rèn)想要上架此書籍嗎?',function(r){if (r){var row = $('#dg').datagrid('getSelected');if (row){$.ajax({url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=2&id=' + row.id,success:function (data) {$('#dg').datagrid('reload');}})} }});}//修改function edit() {$('#cid').combobox({url:'${pageContext.request.contextPath}/category.action?methodName=combobox',valueField:'id',textField:'name'});var row = $('#dg').datagrid('getSelected');if (row) {$('#ff').form('load', row);$('#dd').dialog('open');}}//提交編輯信息的表單function submitForm() {$('#ff').form('submit',{url:'${pageContext.request.contextPath}/book.action?methodName=edit',success: function (param) {$('#dd').dialog('close');$('#dg').datagrid('reload');$('#ff').form('clear');}});}function clearForm() {$('#ff').form('clear');}//圖片上傳function upload() {$('#dd2').dialog('open');}//圖片上傳表單提交function submitForm2() {var row = $('#dg').datagrid('getSelected');console.log(row);// if (row) {// $('#ff2').attr("action", $('#ff2').attr("action") + "&id=" + row.id);// }$('#ff2').form('submit', {url: '${pageContext.request.contextPath}/book.action?methodName=upload&id=' + row.id,success: function (param) {$('#dd2').dialog('close');$('#dg').datagrid('reload');$('#ff2').form('clear');}})}$(function () {$("#btn-search").click(function () {$('#dg').datagrid('load', {name: $("#name").val()});});$('#dg').datagrid({url: '${pageContext.request.contextPath}/bookVo.action?methodName=list&&state=1',fit: true,fitColumns: true,pagination: true,singleSelect: true,toolbar:'#tb',columns: [[// {field:'id',title:'id',width:100},{field: 'id', title: '書籍名稱', hidden: true},{field: 'name', title: '書籍名稱', width: 50},{field: 'pinyin', title: '拼音', width: 80},{field: 'cname', title: '連表類別', width: 40},{field: 'cid', title: '書籍類別', width: 40, formatter: function (value, row, index) {//方法一:1,同步異步問題 2,json對(duì)象轉(zhuǎn)json字符串問題 3,性能問題//書籍類別id var cid = row.cid;var typeName = "";$.ajax({url: '${pageContext.request.contextPath}/category.action?methodName=load&&id='+cid,//同步異步 async:false, success:function(data){//轉(zhuǎn)換為json字符串 var jsonObj = eval("("+data+")");typeName = jsonObj.name;//alert(typeName);}});return typeName;}},{field: 'author', title: '作者', width: 40},// {field:'price',title:'價(jià)格',width:100},{field: 'image', title: '圖片路徑', width: 42, formatter: function (value, row, index) {return '<img style="width:80px;height: 60px;" src="' + row.image + '"></img>';}},{field: 'publishing', title: '出版社', width: 40},{field: 'price', title: '價(jià)格', width: 40},// {field:'desc',title:'描述',width:100},// {field:'state',title:'書籍狀態(tài)',width:100},{field: 'sales', title: '銷量', width: 40},{field: 'description', title: '簡(jiǎn)介', width: 80},{field: 'deployTime', title: '上架時(shí)間', width: 80, align: 'right'},{field: 'xxxx', title: '操作', width: 80, formatter: function (value, row, index) {return '<a href="#" οnclick="upload()">圖片上傳</a> ' +'<a href="#" οnclick="shangjia()">上架</a> ' +'<a href="#" οnclick="edit();">修改</a>';}}]]});})</script> </html>下架界面代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>已上架書籍</title><link rel="stylesheet" type="text/css"href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css"><script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script><script type="text/javascript"src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script><script src="${pageContext.request.contextPath}/static/js/main.js"></script> </head> <body> <table id="dg" style="style=" width:400px;height:200px; "></table><script>function xiajia() {$.messager.confirm('確認(rèn)','您確認(rèn)想要下架此書籍嗎?',function(r){if (r){var row = $('#dg').datagrid('getSelected');if (row){$.ajax({url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=3&id=' + row.id,success:function (data) {$('#dg').datagrid('reload');}})}}});}$(function () {$('#dg').datagrid({url: '${pageContext.request.contextPath}/book.action?methodName=list&&state=2',fit: true,fitColumns: true,pagination: true,singleSelect: true,columns: [[// {field:'id',title:'id',width:100},{field: 'id', title: '書籍名稱', hidden: true},{field: 'name', title: '書籍名稱', width: 50},{field: 'pinyin', title: '拼音', width: 80},{field: 'cid', title: '書籍類別', width: 40, formatter: function (value, row, index) {if (row.cid == 1) {return "文藝";} else if (row.cid == 2) {return "小說";} else if (row.cid == 3) {return "青春";} else {return value;}}},{field: 'author', title: '作者', width: 40},// {field:'price',title:'價(jià)格',width:100},{field: 'image', title: '圖片路徑', width: 41, formatter: function (value, row, index) {return '<img style="width:80px;height: 60px;" src="' + row.image + '"></img>';}},{field: 'publishing', title: '出版社', width: 40},// {field:'desc',title:'描述',width:100},// {field:'state',title:'書籍狀態(tài)',width:100},{field: 'sales', title: '銷量', width: 40},{field: 'deployTime', title: '上架時(shí)間', width: 80, align: 'right'},{field: 'xxxx', title: '操作', width: 80, formatter: function (value, row, index) {return '<a href="#" οnclick="xiajia();">下架</a>';}}]]});}) </script> </body> </html>效果展示:
三,修改
BookDao
// 修改public void edit( Book book) throws Exception {//設(shè)置拼音book.setPinyin(PinYinUtil.getAllPingYin(book.getName()));//設(shè)置時(shí)間book.setDeployTime(new Date());String sql = "update t_easyui_book set name=?,pinyin=?,cid=?,image=?,state=?,sales=?,author=?,publishing=? where id=?";super.executeUpdate(sql, book, new String[] {"name","pinyin","cid","image","state","sales","author","publishing","id"});}BookAction
public void edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {try {bookDao.edit(book);ResponseUtil.writeJson(resp, 1);} catch (Exception e) {e.printStackTrace();ResponseUtil.writeJson(resp, 0);}}js代碼
//修改function edit() {$('#cid').combobox({url:'${pageContext.request.contextPath}/category.action?methodName=combobox',valueField:'id',textField:'name'});var row = $('#dg').datagrid('getSelected');if (row) {$('#ff').form('load', row);$('#dd').dialog('open');}}//提交編輯信息的表單function submitForm() {$('#ff').form('submit',{url:'${pageContext.request.contextPath}/book.action?methodName=edit',success: function (param) {$('#dd').dialog('close');$('#dg').datagrid('reload');$('#ff').form('clear');}});}function clearForm() {$('#ff').form('clear');}效果展示:
到這里就結(jié)束了,有不對(duì)或補(bǔ)充的地方歡迎大家評(píng)論,謝謝!
總結(jié)
以上是生活随笔為你收集整理的EasyUI项目之书籍管理(CRUD)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB命令大全和矩阵操作大全
- 下一篇: 实打实的