【EasyUI之crud使用】

前言

先讲下crud是什么好吧。

CRUD是指在做计算处理时的增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。

哈哈,其实也就是对一个表的增,删,查,改。

那么今天是要用到数据表格(datagrid),用来装填,用弹出框(dialog)来操作数据,用表单(from)来提交数据

那么,今天的内容也就明了


数据表格(datagrid):

前端设计

咱们来看看效果图
在这里插入图片描述
这就是数据表格
在这里插入图片描述这上面的工具是数据表格的一个组件,
那么一个视图的实现必定有他代码支撑

首先看下jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<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 }/jsp/userManage.js"></script>

</head>
<body>
	<input type="hidden" id="ctr" value="${pageContext.request.contextPath }">
	
	<table id="dg"></table>  
	 
</body>
</html>

一个工具栏插件:

$(function(){
	$('#dg').datagrid({    
	    url:$("#ctr").val()+'/userAction.action?methodName=list', //有jsp来获取的绝对路径
	    fitColumns:true,
	    pagination:true,
	    fit:true,
	    columns:[[    
	        {field:'productid',title:'代码',width:100},    
	        {field:'productname',title:'名称',width:100},    
	        {field:'unitcost',title:'价格',width:100,align:'right'}    
	    ]],
	    toolbar: [{//看这里看这里工具栏的插件
			iconCls: 'icon-edit',
			handler: function(){//编辑
		},'-',{
			iconCls: 'icon-add',
			handler: function(){	//增加
			}
		},'-',{
			iconCls: 'icon-remove',
			handler: function(){//删除
				}
				
				
			}
		}]
	    
	});

弹出框(dialog):

前端设计

一个xml配置:

<body>
	
	<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">   
   	
	</div> 
</body>

对了,忘说了,userManage.js是用来放本节面的js代码哦

所以到js文件:

	$('#dd').dialog({
		buttons:[{//也是一种插件按钮插件
			text:'保存',
			handler:function(){}
		},{
			text:'关闭',
			handler:function(){}
		}]
	})

这个会比较少,

表单(from)

前端设计

说到表单我们都知道是用来发送请求的嘛。
所以这次我们要的是放到弹出框上去

	<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">   
   		<form id="ff" method="post">   
   			<input type="hidden" name="SerialNo" >
		   uid<input type="text" id="uid" name="uid"  value="" class="easyui-textbox" style="width:200px"><br>
		   uname<input type="text" id="uname" name="uname" value="" class="easyui-textbox"  style="width:200px"><br>
		   upwd<input type="password" id="upwd" name="upwd" value="" class="easyui-textbox"  style="width:200px"><br>   
		</form>  
	</div> 

就是这个:
在这里插入图片描述
嵌套了在弹出框里;

function ok(){
		$('#ff').form('submit',{//表单提交方法    
		    url:$("#ctr").val()+'/userAction.action?methodName='+$("#str").val(),    
		    onSubmit: function(){ 
		    	
		    },    
		    success:function(data){    
		    }
		}); 
	}

后端以及前端代码使用

userDao

其实我多搞得麻烦了一些,就是哪个zhu()方法,如果明白的可以不加

public class UserDao extends JsonBaseDao{
	
	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");//获取参数值uid
		String upwd = JsonUtils.getParamVal(pamap, "upwd");//获取参数值upwd
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid = "+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql +=" and upwd = "+upwd;
		}
		
		return super.executeQuery(sql, pageBean);
	}
	
	public List<Map<String,Object>> listmenu(Map<String, String[]> pamap ,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_usermenu where true ";
		String uid = JsonUtils.getParamVal(pamap, "uid");
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid = "+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
	
	public List<Map<String,Object>> list2(Map<String, String[]> pamap ,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_user_version2 where true ";
		
		return super.executeQuery(sql, pageBean);
	}
	
	
	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);
	}
	public int add(Map<String, String[]> pamap ) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException {
		List<Map<String, Object>> list2 = this.list2(pamap, null);
		String sql = "insert into  t_easyui_user_version2(SerialNo,uid,uname,upwd) values("+list2.size()+1+",?,?,?) ";
		
		return super.executeUpdate(sql,new String[] {"uid","uname","upwd"}, pamap);
	}
	public int del(Map<String, String[]> pamap ) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql = "delete from  t_easyui_user_version2 where uid = ?";
		
		return super.executeUpdate(sql,new String[] {"uid"}, pamap);
	}
	
	
	public List<Map<String,Object>> zhu(List<Map<String,Object>> pamap){
		List<Map<String,Object>> la = new ArrayList<>();
		Map<String, Object> ma ;
		for (Map<String, Object> map : pamap) {
			ma = new HashMap<>();
			ma.put("productid", map.get("uid"));
			ma.put("productname", map.get("uname"));
			ma.put("unitcost", map.get("upwd"));
			ma.put("status", map.get("SerialNo"));
			la.add(ma);
		}
		
		return la;
	}
}

这个方法是在json文件里的格式,其实可以通过修改json文件格式就简单些

public List<Map<String,Object>> zhu(List<Map<String,Object>> pamap){
		List<Map<String,Object>> la = new ArrayList<>();
		Map<String, Object> ma ;
		for (Map<String, Object> map : pamap) {
			ma = new HashMap<>();
			ma.put("productid", map.get("uid"));
			ma.put("productname", map.get("uname"));
			ma.put("unitcost", map.get("upwd"));
			ma.put("status", map.get("SerialNo"));
			la.add(ma);
		}
		
		return la;
	}

UserAction

public class UserAction extends ActionSupport{
	
	private UserDao ud = new UserDao();
	
	public String login(HttpServletRequest req,HttpServletResponse resp) {
			try {
				List<Map<String, Object>> list = ud.list(req.getParameterMap(), null);//获取数据库中有无这个人
				
				if(list!=null&&list.size()>0) {//如果有,则进入下一步
					List<Map<String, Object>> listmenu = ud.listmenu(req.getParameterMap(), null);//获取这个人的权限
					StringBuilder sb = new StringBuilder();
					for (Map<String, Object> map : listmenu) {//将权限已字符串的形式打包
						sb.append(","+map.get("menuId"));
					}
					req.setAttribute("menuHid", sb.substring(1));//设置到request中
					
				}else {
					return "login";//如果没有就到login.jsp中
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		return "index";//如果有这个人就到index.jsp中
	}
	
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		try {
			
			PageBean pageBean = new PageBean();//分页
			pageBean.setRequest(req);
			
			List<Map<String, Object>> listmenu = ud.list(req.getParameterMap(), pageBean);//获取所信息
			List<Map<String, Object>> zhulist = ud.zhu(listmenu);//转换格式
			Map<String, Object> ma = new HashMap<>();
			ma.put("total", pageBean.getTotal());
			ma.put("rows", zhulist);//符合json文件的格式
			ObjectMapper om = new ObjectMapper();
			ResponseUtil.write(resp, om.writeValueAsString(ma));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	return null;
}
	public String edit(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int edit = ud.edit(req.getParameterMap());
			ObjectMapper om = new ObjectMapper();
			ResponseUtil.write(resp, om.writeValueAsString(edit));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	return null;
}
	public String add(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int edit = ud.add(req.getParameterMap());
			ObjectMapper om = new ObjectMapper();
			ResponseUtil.write(resp, om.writeValueAsString(edit));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	return null;
}
	public String del(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int edit = ud.del(req.getParameterMap());
			ObjectMapper om = new ObjectMapper();
			ResponseUtil.write(resp, om.writeValueAsString(edit));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	return null;
}
}

最终版的前端代码

userManage.jsp

<body>
	<input type="hidden" id="ctr" value="${pageContext.request.contextPath }">
	<input type="hidden" id="str"  >
	
	<table id="dg"></table>  
	<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">   
   		<form id="ff" method="post">   
   			<input type="hidden" name="SerialNo" >
		   uid<input type="text" id="uid" name="uid"  value="" class="easyui-textbox" style="width:200px"><br>
		   uname<input type="text" id="uname" name="uname" value="" class="easyui-textbox"  style="width:200px"><br>
		   upwd<input type="password" id="upwd" name="upwd" value="" class="easyui-textbox"  style="width:200px"><br>
		   
		</form>  
	</div> 
	
	 
</body>

userManage.js

$(function(){
	$('#dg').datagrid({    
	    url:$("#ctr").val()+'/userAction.action?methodName=list', 
	    fitColumns:true,
	    pagination:true,
	    fit:true,
	    columns:[[    
	        {field:'productid',title:'代码',width:100},    
	        {field:'productname',title:'名称',width:100},    
	        {field:'unitcost',title:'价格',width:100,align:'right'}    
	    ]],
	    toolbar: [{
			iconCls: 'icon-edit',
			handler: function(){
				var row = $('#dg').datagrid('getSelected');
				if(row!=null){
					$('#ff').form('clear');
					$("#str").val('edit');
					$('#dd').dialog('open');
					$('#ff').form('load',{
						uid:row.productid,
						uname:row.productname,
						upwd:row.unitcost,
						SerialNo:row.status
					});
					
				}
				
			}
		},'-',{
			iconCls: 'icon-add',
			handler: function(){
				$('#ff').form('clear');
				$('#str').val('add');
				$('#dd').dialog('open');
				
			}
		},'-',{
			iconCls: 'icon-remove',
			handler: function(){
				var row = $('#dg').datagrid('getSelected');
				if(row!=null){
					$('#str').val('del');
					$("uid").val(row.productid);
					ok();
					
				}
				
				
			}
		}]
	    
	});
	
	$('#dd').dialog({
		buttons:[{
			text:'保存',
			handler:function(){
				ok();  
			}
		},{
			text:'关闭',
			handler:function(){}
		}]
	})
	
	function ok(){
		$('#ff').form('submit',{    
		    url:$("#ctr").val()+'/userAction.action?methodName='+$("#str").val(),    
		    onSubmit: function(){ 
		    	
		    },    
		    success:function(data){    
		        if(data>>0){
		        	$('#dd').dialog('close');
		        	$('#dg').datagrid('reload');
		        	alert('编辑成功');
		        	$('#ff').form('clear');
		        	
		        }
		        else{
		        	alert('编辑失败');
		        }
		    }    
		}); 
	}
	


})

一上代码搞好就可以,实现工具栏的方法实现


总结

总的说不难,思路可以有很多种

☋希望大家有所帮助

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值