1、easyui的crud(datagrid、dialog、form)
数据表格是基于table标签的
1、datagrid(数据表格)
$(’#dg’).datagrid({
url:‘datagrid_data.json’,
columns:[[
{field:‘code’,title:‘代码’,width:100},
{field:‘name’,title:‘名称’,width:100},
{field:‘price’,title:‘价格’,width:100,align:‘right’}
]]
});
数据 ‘datagrid_data.json’ 可以是指定的 json文件,也可以是从后台传输过来的 json 对象。
{"total":28,"rows":[
{"uid":"FI-SW-01","uname":"Koi","upwd":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"uid":"K9-DL-01","uname":"Dalmation","upwd":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"uid":"RP-SN-01","uname":"Rattlesnake","upwd":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
{"uid":"RP-SN-01","uname":"Rattlesnake","upwd":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"uid":"RP-LI-02","uname":"Iguana","upwd":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"uid":"FL-DSH-01","uname":"Manx","upwd":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"uid":"FL-DSH-01","uname":"Manx","upwd":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"uid":"FL-DLH-02","uname":"Persian","upwd":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
{"uid":"FL-DLH-02","uname":"Persian","upwd":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"uid":"AV-CB-01","uname":"Amazon Parrot","upwd":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
]}
2、Dialog(对话框窗口)
使用Javascript创建对话框窗口也是允许的。现在让我们创建一个模式窗口并调用’refresh’方法通过ajax读取内容。
$('#dd').dialog({
title: 'My Dialog',
width: 400,
height: 200,
closed: false,
cache: false,
href: 'get_content.php',
modal: true
});
$('#dd').dialog('refresh', 'new_content.php');
3、Form(表单)
<form id="ff" method="post">
<input type="hidden" name="SerialNo">
<div>
<label for="name">Uid:</label> <input class="easyui-validatebox"
type="text" name="uid" data-options="required:true" />
</div>
<div>
<label for="name">Name:</label> <input class="easyui-validatebox"
type="text" name="uname" data-options="required:true" />
</div>
<div>
<label for="name">Upwd:</label> <input class="easyui-validatebox"
type="text" name="upwd" data-options="required:true" />
</div>
</form>
陈旧的开发模式
美工(ui工程师:出一个项目模型)
java工程师:将原有的html转成jsp,动态展示数据
缺点:
客户需要调节前端的展示效果
解决:由美工去重新排版,重新选色。
Vs
前后端分离
美工、java工程师都是独立工作的,彼此之间在开发过程中是没有任何交际。
在开发前约定数据交互的格式。
java工程师的工作:写方法返回数据如tree_data1.json
美工:只管展示tree_data1.json
DataGrid 数据表格
DataGrid以表格形式展示数据,并提供了丰富的选择、排序、分组、分页和编辑数据的功能支持。DataGrid的设计用于缩短开发时间,并且使开发人员不需要具备特定的知识。它是轻量级的且功能丰富。
我们先写DataGrid的静态界面:
<%@ 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">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/js/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/userManage.js"></script>
<title>userManage</title>
</head>
<body>
<!-- 布局:北North -->
<div data-options="region:'north',title:'',split:true,border:false"
style="height: 80px; align-content: center;">
<!-- 搜索功能 -->
<div style="height: 40px; line-height: 80px">
<label>用户名称:</label> <input type="text" id="username" /> <a
id="btn_search" href="#" class="easyui-linkbutton"
data-options="iconCls: 'icon-search'">列表查询</a>
</div>
</div>
<!-- 展示数据所用 -->
<table id="dg"></table>
<input type="hidden" id="ctx"
value="${pageContext.request.contextPath }">
<input type="hidden" id="name">
<!-- 弹出框提交表单所用 -->
<div id="dd" class="easyui-dialog" title="My Dialog"
style="width: 400px; height: 200px;"
data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'">
<form id="ff" method="post">
<input type="hidden" name="SerialNo">
<div>
<label for="name">Uid:</label> <input class="easyui-validatebox"
type="text" name="uid" data-options="required:true" />
</div>
<div>
<label for="name">Name:</label> <input class="easyui-validatebox"
type="text" name="uname" data-options="required:true" />
</div>
<div>
<label for="name">Upwd:</label> <input class="easyui-validatebox"
type="text" name="upwd" data-options="required:true" />
</div>
</form>
</div>
<div id="bb">
<a class="easyui-linkbutton" onclick="ok();">保存</a> <a href="#"
class="easyui-linkbutton">关闭</a>
</div>
<!-- <div id="tb"> -->
<!-- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a> -->
<!-- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true"/a> -->
<!-- </div> -->
</body>
</html>
然后写前端的动态js,动态js包括了我们的增删查改方法。
userManage.js:
$(function() {
$('#dg').datagrid({
url : $("#ctx").val() + '/userAction.action?methodName=list',
fitColumns : true, // 列宽自适应
singleSelect : true, // 是否选中单行
fit : true, // 高宽自适应
border : false, // 是否显示边框
rownumbers : true, // 行号
striped : true, // 显示斑马线效果
pagination : true, // 显示分页工具栏
columns : [ [ {
field : 'uid',
title : '代码',
width : 100
}, {
field : 'uname',
title : '名称',
width : 100
}, {
field : 'upwd',
title : '价格',
width : 100,
align : 'right'
} ] ],
toolbar : [ {
iconCls : 'icon-edit',
handler : function() {
// 获取选中的第一行的数据
var row = $('#dg').datagrid('getSelected');
if (row != null) {
// 回显到表单里
$('#ff').form('load', row);
// 打开窗体
$('#dd').dialog('open');
$('#name').val('edit');
} else {
alert('请选中你要修改的项!');
}
}
}, '-', {
iconCls : 'icon-add',
handler : function() {
$('#ff').form('clear');
$('#name').val('add');
$('#dd').dialog('open');
}
}, '-', {
iconCls : 'icon-remove',
handler : function() {
var row = $('#dg').datagrid('getSelected');
if (row) {
if (confirm('你确定要删除吗?')) {
// 回显到表单里
$('#ff').form('load', row);
$('#name').val('del');
ok();
}
} else {
alert('请选中你要删除的项!');
}
}
} ],
});
})
function ok() {
$('#ff').form(
'submit',
{
url : $("#ctx").val() + '/userAction.action?methodName='
+ $('#name').val(),
success : function(param) {
if (param > 0) {
alert($('#name').text() + '成功!');
$('#dd').dialog('close');
$('#dg').datagrid('reload');
$('#ff').form('clear');
}
if (param == 0) {
alert($('#name').text() + '失败!');
}
}
});
}
这是 DataGrid 的对话框窗口
前端的静动态代码块就差不多了,我们来写后台的Dao方法:
package com.xzy.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.xzy.util.JsonBaseDao;
import com.xzy.util.JsonUtils;
import com.xzy.util.PageBean;
import com.xzy.util.StringUtils;
public class UserDao extends JsonBaseDao {
/**
* 登入查询用户表 登入
*
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]> paMap, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_user_version2 where true";
String uid = JsonUtils.getParamVal(paMap, "uid");
String upwd = JsonUtils.getParamVal(paMap, "upwd");
if (StringUtils.isNotBlank(uid)) {
sql = sql + " and uid = " + uid;
}
if (StringUtils.isNotBlank(upwd)) {
sql = sql + " and upwd = " + upwd;
}
return super.executeQuery(sql, pageBean);
}
/**
* 通过中间表查询登入用户所对应的权限
*
* @param paMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMenu(String uid, PageBean pageBean)
throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_easyui_usermenu where true";
if (StringUtils.isNotBlank(uid)) {
sql = sql + " and uid = " + uid;
}
return super.executeQuery(sql, pageBean);
}
/**
* 修改
*
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "update t_easyui_user_version2 set uid=?,uname=?,upwd=? where serialno=?";
return super.executeUpdate(sql, new String[] { "uid", "uname", "upwd", "SerialNo" }, paMap);
}
/**
* 增加
*
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "insert into t_easyui_user_version2 (uid,uname,upwd) values(?,?,?)";
return super.executeUpdate(sql, new String[] { "uid", "uname", "upwd" }, paMap);
}
/**
* 删除
*
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int del(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "delete from t_easyui_user_version2 where SerialNo=?";
return super.executeUpdate(sql, new String[] { "SerialNo" }, paMap);
}
}
写好Dao方法后,我们要去web层去实现我们写好的Dao方法,然后在到mvc.xml配置路径,
web层:userAction:
package com.xzy.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xzy.dao.UserDao;
import com.xzy.util.PageBean;
import com.xzy.util.ResponseUtil;
import com.zking.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao userDao = new UserDao();
public String login(HttpServletRequest req, HttpServletResponse resp) {
try {
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
if (list != null && list.size() > 0) {
List<Map<String, Object>> listMenu = this.userDao.listMenu(req.getParameter("uid"), null);
StringBuilder sb = new StringBuilder();
for (Map<String, Object> map : listMenu) {
sb.append("," + map.get("menuId"));
}
// ,001,002
req.setAttribute("menuHid", sb.substring(1));
} else {
return "login";
}
} catch (Exception e) {
e.printStackTrace();
}
return "index";
}
/**
* datagrid所需数据后端程序员开发完毕
*
* @param req
* @param resp
* @return
*/
public String list(HttpServletRequest req, HttpServletResponse resp) {
try {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
map.put("total", pageBean.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* form组建提交所需要数据后端程序员处理完毕
*
* @param req
* @param resp
* @return
*/
public String edit(HttpServletRequest req, HttpServletResponse resp) {
try {
int edit = this.userDao.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(edit));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 增加
*
* @param req
* @param resp
* @return
*/
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
int add = this.userDao.add(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(add));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param req
* @param resp
* @return
*/
public String del(HttpServletRequest req, HttpServletResponse resp) {
try {
int del = this.userDao.del(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(del));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
mvc.xml: 只要配置这个就OK啦,但是你要检查下你的路径。因为我们所写的userAction的增加,修改,删除方法都不需要跳转页面,所以return null 值,我们在mvc.xml中就不需要配置了。
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/userAction" type="com.xzy.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
</action>
</config>
好了,最后我们来看下我们的效果图:
这是最原始的展示界面:
我们来增加一个UID为006的用户:
其次我们对增加的这位用户修改下信息:
我们在把修改后的这位用户给删除:体验性高一点点。怕会是出现误删的情况,但是当你点击确定的时候这位客户就会被删除掉。
好,测试完毕,我们回到了最原始的界面。。
DataGrid 数据表格 算是一个重点内容了。谢谢参考