easyui实现增删查改

easyui实现增删查改


在实现增删查改之前,我们先要完成前端的布局,需要使用到easyui的datagrid,dialog,form这三个布局。

roleInfo.jsp

<body>
<!-- 展示数据 -->
<table id="dg"></table>  
<input type="hidden" id="ctx" value="${pageContext.request.contextPath}">
<input type="hidden" id="shl" value="11">
<!-- 弹出框提交表单 -->
<div id="dd" class="easyui-dialog" title="编辑窗体" 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="uid">uid:</label>   
        <input class="easyui-validatebox" type="text" name="uid" data-options="required:true" />   
    </div>   
     <div>   
        <label for="uname">uname:</label>   
        <input class="easyui-validatebox" type="text" name="uname" data-options="required:true" />   
    </div>   
     <div>   
        <label for="upwd">upwd:</label>   
        <input class="easyui-validatebox" type="text" name="upwd" data-options="required:true" />   
    </div>   
</form>  
</div>  
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="ok();">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
</body>

roleInfo.js

$(function(){
	$('#dg').datagrid({    
	    url:$("#ctx").val()+'/userAction.action?methodName=list',    
	    fitColumns:true,//填充列
	    fit: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){
					$('#shl').val("edit");//设置修改方法的路径
					$('#ff').form("load",row);
					$('#dd').dialog("open");
					alert($('#shl').val());
				}
			}
		},'-',{
			iconCls: 'icon-add',
			handler: function(){
				$('#ff').form("clear");
				$('#dd').dialog("open");
				$('#shl').val("add");//设置增加方法的路径
				$('#dg').datagrid("reload");
			}
		},'-',{
			iconCls: 'icon-remove',
			handler: function(){
				var row=$('#dg').datagrid("getSelected");
				$.ajax({
					 url:$("#ctx").val()+'/userAction.action?methodName=del&&SerialNo='+row.SerialNo,    
				     success: function(param){    
				         $('#dg').datagrid("reload");
				     } 
				})
			}
		}]

	});  
})
//表单提交
function ok(){
	$('#ff').form('submit', {    
	    url:$("#ctx").val()+'/userAction.action?methodName='+$('#shl').val(),    
	    success: function(param){    
	        $('#dd').dialog("close");
	        $('#dg').datagrid("reload");
	        $('#ff').form("clear");
	    }    
	});  
}

UserDao

package com.shl.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.shl.util.JsonBaseDao;
import com.shl.util.JsonUtils;
import com.shl.util.PageBean;
import com.shl.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 pb) 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, pb);
	}
	
	/**
	 * 通过中间表查询登录用户所对应的权限
	 * @param paMap
	 * @param pb
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenu(String uid,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_user_version2 where true ";
		if(StringUtils.isNotBlank(uid)) {
			sql=sql+" and uid = "+uid;
		}
		return super.executeQuery(sql, pb);
	}
	
	/**
	 * 修改
	 * @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 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);
	}
	
	/**
	 * 增加
	 * @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);
	}
}

UserAction

package com.shl.web;

import java.sql.SQLException;
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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shl.dao.UserDao;
import com.shl.util.PageBean;
import com.shl.util.ResponseUtil;
import com.zking.framework.ActionSupport;

public class UserAction extends ActionSupport {
	
	private UserDao ud=new UserDao();
	
	public String login(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), null);
		if(list!=null&&list.size()>0) {
			List<Map<String, Object>> listMenu = this.ud.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";
		}
		return "index";
	}
	
	/**
	 * datagrid所需数据
	 * @param req
	 * @param resp
	 * @return
	 * @throws JsonProcessingException
	 * @throws Exception
	 */
	public String list(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		PageBean pb=new PageBean();
		pb.setRequest(req);
		List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), pb);
		ObjectMapper om=new ObjectMapper();
		Map<String, Object> map=new HashMap<>();
		map.put("total", pb.getTotal());
		map.put("rows", list);
		ResponseUtil.write(resp, om.writeValueAsString(map));
		return null;
	}
	
	/**
	 * form组件提交所需数据(修改)
	 * @param req
	 * @param resp
	 * @return
	 * @throws JsonProcessingException
	 * @throws Exception
	 */
	public String edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		int edit=this.ud.edit(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(edit));
		return null;
	}
	
	/**
	 * 删除
	 * @param req
	 * @param resp
	 * @return
	 * @throws JsonProcessingException
	 * @throws Exception
	 */
	public String del(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		int del=this.ud.del(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(del));
		return null;
	}
	
	/**
	 * 增加
	 * @param req
	 * @param resp
	 * @return
	 * @throws JsonProcessingException
	 * @throws Exception
	 */
	public String add(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		int add=this.ud.add(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(add));
		return null;
	}
}

这样我们的增删查改就完成了
效果:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以回答这个问题。以下是一个简单的 easyui 增删实现的示例代码: HTML 代码: ``` <div class="easyui-layout" style="width:100%;height:100%;"> <div region="west" split="true" title="菜单" style="width:200px;"> <ul id="menu"></ul> </div> <div region="center" title="内容"> <table id="dg"></table> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a> </div> </div> </div> ``` JS 代码: ``` $(function(){ $('#menu').tree({ url:'menu.json', onClick:function(node){ $('#dg').datagrid({ url:'get_users.php?menu='+node.id, fit:true, fitColumns:true, singleSelect:true, rownumbers:true, pagination:true, toolbar:'#toolbar', columns:[[ {field:'id',title:'编号',width:50}, {field:'name',title:'姓名',width:100}, {field:'sex',title:'性别',width:50}, {field:'age',title:'年龄',width:50}, {field:'phone',title:'电话',width:100}, {field:'email',title:'邮箱',width:150}, {field:'address',title:'地址',width:200}, {field:'remark',title:'备注',width:200} ]] }); } }); }); function newUser(){ $('#dlg').dialog('open').dialog('setTitle','新'); $('#fm').form('clear'); url = 'save_user.php'; } function editUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $('#dlg').dialog('open').dialog('setTitle','编辑'); $('#fm').form('load',row); url = 'update_user.php?id='+row.id; } } function saveUser(){ $('#fm').form('submit',{ url: url, onSubmit: function(){ return $(this).form('validate'); }, success: function(result){ var result = eval('('+result+')'); if (result.errorMsg){ $.messager.show({ title: 'Error', msg: result.errorMsg }); } else { $('#dlg').dialog('close'); $('#dg').datagrid('reload'); } } }); } function destroyUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $.messager.confirm('Confirm','Are you sure you want to delete this user?',function(r){ if (r){ $.post('destroy_user.php',{id:row.id},function(result){ if (result.success){ $('#dg').datagrid('reload'); } else { $.messager.show({ title: 'Error', msg: result.errorMsg }); } },'json'); } }); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值