【combogrid组件】-001-表格搜索案例

本文档详细介绍了如何使用EasyUI的Combogrid组件实现表格搜索功能。通过展示界面,解析jsp和servlet代码,以及DAO层的交互,展示了完整的搜索案例。
摘要由CSDN通过智能技术生成

1、界面展示



 

 

2、WebContent/jsp/combogrid_001.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String root = request.getContextPath();
%>
<!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">
<title>【combogrid组件】-001-表格搜索案例</title>
<!-- 引入外部样式 -->
<link rel="stylesheet" type="text/css" href="<%=root %>/css/common.css" />
<!-- 引入easyui依赖库 -->
<script type="text/javascript" src="<%=root %>/js/jquery-easyui-1.2.6/jquery-1.7.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=root %>/js/jquery-easyui-1.2.6/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="<%=root %>/js/jquery-easyui-1.2.6/themes/icon.css" />
<script type="text/javascript" src="<%=root %>/js/jquery-easyui-1.2.6/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=root %>/js/jquery-easyui-1.2.6/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	$(function() {
		//表格自动搜索 
		$('#cc').combogrid({
			panelWidth:450,
			idField:'id',
			textField:'username',
			mode:'remote' , //remote表示从服务器加载数据
			url:'<%=root%>/UserServlet?method=searchName', //请求地址
			delay:300, //延迟搜索的时间
			columns:[[
				{field:'username',title:'用户名',width:100},
				{field:'age',title:'年龄',width:50},
				{
					field:'sex',
					title:'性别',
					width:50, 
					formatter:function(value,record,index){
						 if(value==1){
							 return '<span style=color:red;>男</span>';
						 } else if(value ==2){
							 return '<span style=color:green;>女</span>';
						 }
					}
				},
				{field:'salary',title:'薪水',width:100}
			]]
		});
	});
</script>
</head>
<body>
	表格自动搜索:<select id="cc" name="gridId" style="width:250px;"></select>
</body>
</html>

 

3、com.easyui.servlet.UserServlet.java

 

package com.easyui.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.easyui.bean.CityBean;
import com.easyui.bean.TUserBean;
import com.easyui.dao.UserDao;

/**
 * 用户控制器类
 * 
 * @author LiPiaoShui
 */
public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 9140830946116659042L;
	private UserDao uDao = new UserDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if ("getList".equals(method)) {
			getList(request, response);
		} else if("getCityName".equals(method)) {
			getCityName(request, response);
		} else if("getCity".equals(method)) {
			getCity(request, response);
		} else if("save".equals(method)) {
			save(request, response);
		} else if("update".equals(method)) {
			update(request, response);
		} else if("delete".equals(method)) {
			delete(request, response);
		} else if("searchName".equals(method)) {
			searchName(request, response);
		}
	}
	
	/**
	 * 根据名字查询用户信息
	 * @param request
	 * @param response
	 */
	private void searchName(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//默认前台送过来的是p参数
			String q = request.getParameter("q") == null ? "" : request.getParameter("q");
			List<TUserBean> uList = this.uDao.searchByName(q.trim());
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(JSONArray.fromObject(uList).toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除用户信息
	 * @param request
	 * @param response
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		try {
			String[] ids = request.getParameter("ids").split(",");
			int count = 0;
			for(String id:ids) {
				count += this.uDao.deleteByUserId(Integer.parseInt(id));
			}
			String json = "{\"status\":\"提示信息\",\"message\":\"删除成功,总共删除"+count+"条记录\"}";
			if(count <= 0) {
				json = "{\"status\":\"提示信息\",\"message\":\"删除失败\"}";
			}
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改用户信息
	 * @param request
	 * @param response
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		TUserBean user = new TUserBean();
		user = this.uDao.queryUserById(id);
		//获取前台传入的信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		String birthday = request.getParameter("birthday");
		int city = Integer.parseInt(request.getParameter("city"));
		String salary = request.getParameter("salary");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String description = request.getParameter("description");
		//设置用户信息
		user.setUsername(username);
		user.setPassword(password);
		user.setSex(sex);
		user.setAge(age);
		user.setBirthday(birthday);
		user.setCity(city);
		user.setSalary(salary);
		user.setStarttime(starttime);
		user.setEndtime(endtime);
		user.setDescription(description);
		try {
			//更新用户信息
			int count = uDao.update(user);
			String json = "{\"status\":\"提示信息\",\"message\":\"更新成功\"}";
			if(count != 1) {
				json = "{\"status\":\"提示信息\",\"message\":\"更新失败\"}";
			}
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存用户信息
	 * @param request
	 * @param response
	 */
	private void save(HttpServletRequest request, HttpServletResponse response) {
		//获取前台传入的信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		String birthday = request.getParameter("birthday");
		int city = Integer.parseInt(request.getParameter("city"));
		String salary = request.getParameter("salary");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String description = request.getParameter("description");
		//设置用户信息
		TUserBean user = new TUserBean();
		user.setUsername(username);
		user.setPassword(password);
		user.setSex(sex);
		user.setAge(age);
		user.setBirthday(birthday);
		user.setCity(city);
		user.setSalary(salary);
		user.setStarttime(starttime);
		user.setEndtime(endtime);
		user.setDescription(description);
		try {
			//保存用户信息
			int count = uDao.save(user);
			String json = "{\"status\":\"提示信息\",\"message\":\"保存成功\"}";
			if(count != 1) {
				json = "{\"status\":\"提示信息\",\"message\":\"保存失败\"}";
			}
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取全部城市信息
	 * @param request
	 * @param response
	 */
	private void getCity(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<CityBean> cList = new ArrayList<CityBean>();
			cList.add(new CityBean(1,"北京"));
			cList.add(new CityBean(2,"上海"));
			cList.add(new CityBean(3,"深圳"));
			cList.add(new CityBean(4,"长春"));
			response.setContentType("text/html;charset=utf-8");
			//[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"深圳"},{"id":4,"name":"长春"}]
			response.getWriter().write(JSONArray.fromObject(cList).toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取城市信息
	 * @param request
	 * @param response
	 */
	private void getCityName(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<CityBean> cList = new ArrayList<CityBean>();
			cList.add(new CityBean(1,"北京"));
			cList.add(new CityBean(2,"上海"));
			cList.add(new CityBean(3,"深圳"));
			cList.add(new CityBean(4,"长春"));
			int id = Integer.parseInt(request.getParameter("id"));
			for(CityBean city:cList) {
				if(id == city.getId()) {
					response.setContentType("text/html;charset=utf-8");
					response.getWriter().write(JSONObject.fromObject(city).toString());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取全部用户信息
	 * 
	 * @param request
	 * @param response
	 */
	private void getList(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//当前页码
			int currentPage = Integer.parseInt(request.getParameter("page"));
			//每页显示的大小
			int pageSize = Integer.parseInt(request.getParameter("rows"));
			//前台查询条件
			String username = request.getParameter("username") == null ? "" : request.getParameter("username");
			String starttime = request.getParameter("starttime") == null ? "" : request.getParameter("starttime");
			String endtime = request.getParameter("endtime") == null ? "" : request.getParameter("endtime");
			TUserBean user = new TUserBean();
			user.setUsername(username);
			user.setStarttime(starttime);
			user.setEndtime(endtime);
			// 获取分页显示的用户信息
			List<TUserBean> uList = uDao.queryByPagination(currentPage, pageSize, user);
			//获取总用户数
			int total = uDao.getTotal(user);
			// json格式 --> {"total":10,"rows":[{},{}]}
			String json = "{\"total\":" + total + ",\"rows\":"
					+ JSONArray.fromObject(uList).toString() + "}";
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

4、com.easyui.dao.UserDao.java

 

package com.easyui.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.easyui.bean.TUserBean;
import com.easyui.util.DBUtil;

/**
 * 用户数据库操作类 
 * @author LiPiaoShui
 */
public class UserDao {

	/**
	 * 获取全部用户信息
	 * @return
	 */
	public List<TUserBean> getList() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<TUserBean> uList = new ArrayList<TUserBean>();
		try {
			String sql = "select * from t_user";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				TUserBean user = new TUserBean();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
				user.setBirthday(rs.getString("birthday"));
				user.setCity(rs.getInt("city"));
				user.setSalary(rs.getString("salary"));
				user.setStarttime(rs.getString("starttime"));
				user.setEndtime(rs.getString("endtime"));
				user.setDescription(rs.getString("description"));
				uList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return uList;
	}
	
	/**
	 * 分页显示用户信息
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public List<TUserBean> queryByPagination(int currentPage,int pageSize, TUserBean userBean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<TUserBean> uList = new ArrayList<TUserBean>();
		try {
			String sql = "select * from t_user where 1=1";
			if(!"".equals(userBean.getUsername().trim())) {
				sql += " and username like '%" + userBean.getUsername() + "%'";
			}
			if(!"".equals(userBean.getStarttime().trim())) {
				sql += " and starttime >= '" + userBean.getStarttime() + "'";
			}
			if(!"".equals(userBean.getEndtime().trim())) {
				sql += " and endtime >= '" + userBean.getEndtime() + "'";
			}
			sql += " limit ?,?";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, (currentPage-1)*pageSize);
			pstmt.setInt(2, pageSize);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				TUserBean user = new TUserBean();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
				user.setBirthday(rs.getString("birthday"));
				user.setCity(rs.getInt("city"));
				user.setSalary(rs.getString("salary"));
				user.setStarttime(rs.getString("starttime"));
				user.setEndtime(rs.getString("endtime"));
				user.setDescription(rs.getString("description"));
				uList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return uList;
	}
	
	/**
	 * 获取总用户数
	 * @return
	 */
	public int getTotal(TUserBean userBean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int total = 0;
		try {
			String sql = "select count(1) from t_user where 1=1";
			if(!"".equals(userBean.getUsername().trim())) {
				sql += " and username like '%" + userBean.getUsername() + "%'";
			}
			if(!"".equals(userBean.getStarttime().trim())) {
				sql += " and starttime >= '" + userBean.getStarttime() + "'";
			}
			if(!"".equals(userBean.getEndtime().trim())) {
				sql += " and endtime >= '" + userBean.getEndtime() + "'";
			}
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return total;
	}
	
	/**
	 * 保存用户信息
	 * @param user
	 * @return
	 */
	public int save(TUserBean user) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int count = 0;
		try {
			String sql = "insert into t_user(username,password,sex,age,birthday,city,"
					+ "salary,starttime,endtime,description) value(?,?,?,?,?,?,?,?,?,?)";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			pstmt.setString(3, user.getSex());
			pstmt.setInt(4, user.getAge());
			pstmt.setString(5, user.getBirthday());
			pstmt.setInt(6, user.getCity());
			pstmt.setString(7, user.getSalary());
			pstmt.setString(8, user.getStarttime());
			pstmt.setString(9, user.getEndtime());
			pstmt.setString(10, user.getDescription());
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		return count;
	}

	/**
	 * 根据用户ID查询用户信息
	 * @param id
	 * @return
	 */
	public TUserBean queryUserById(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		TUserBean user = new TUserBean();
		try {
			String sql = "select * from t_user where id=?";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
				user.setBirthday(rs.getString("birthday"));
				user.setCity(rs.getInt("city"));
				user.setSalary(rs.getString("salary"));
				user.setStarttime(rs.getString("starttime"));
				user.setEndtime(rs.getString("endtime"));
				user.setDescription(rs.getString("description"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return user;
	}
	
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	public int update(TUserBean user) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int count = 0;
		try {
			String sql = "update t_user set username=?,password=?,sex=?,age=?,birthday=?,city=?,"
					+ "salary=?,starttime=?,endtime=?,description=? where id=?";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			pstmt.setString(3, user.getSex());
			pstmt.setInt(4, user.getAge());
			pstmt.setString(5, user.getBirthday());
			pstmt.setInt(6, user.getCity());
			pstmt.setString(7, user.getSalary());
			pstmt.setString(8, user.getStarttime());
			pstmt.setString(9, user.getEndtime());
			pstmt.setString(10, user.getDescription());
			pstmt.setInt(11, user.getId());
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		return count;
	}
	
	/**
	 * 根据用户ID删除用户信息
	 * @param id
	 * @return
	 */
	public int deleteByUserId(int id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int count = 0;
		try {
			String sql = "delete from t_user where id=?";
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pstmt, conn);
		}
		return count;
	}
	
	/**
	 * 根据名字查询用户信息
	 * @return
	 */
	public List<TUserBean> searchByName(String q) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<TUserBean> uList = new ArrayList<TUserBean>();
		try {
			String sql = "select * from t_user where 1=1";
			if(!"".equals(q)) {
				sql += " and username like '%" + q + "%'";
			}
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				TUserBean user = new TUserBean();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setAge(rs.getInt("age"));
				user.setBirthday(rs.getString("birthday"));
				user.setCity(rs.getInt("city"));
				user.setSalary(rs.getString("salary"));
				user.setStarttime(rs.getString("starttime"));
				user.setEndtime(rs.getString("endtime"));
				user.setDescription(rs.getString("description"));
				uList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return uList;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值