• easyui-datagrid个人实例


     这个实例数据表格的功能,可以实现分页,增删改查功能

    1.user.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    String path = 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>Insert title here</title>
    <link rel="stylesheet" type="text/css"
    href="<%=path%>/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css"
    href="<%=path%>/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="<%=path%>/easyui/demo.css">
    <script type="text/javascript" src="<%=path%>/easyui/jquery-1.7.2.min.js"></script>
    <script type="text/javascript"
    src="<%=path%>/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript">
    $(function() {
    var flag ;//判断新增还是修改

    $('#tab').datagrid({
    title : '用户列表',
    iconCls : 'icon-edit',//图标
    //width : '400px',
    height : 'auto',
    nowrap : false,//当true的时候,会显示在一行里
    striped : true,//各行变色
    border : true,
    collapsible : false,//是否可折叠的
    fit : true,//自动大小
    url : '/egoTest/user/queryUser.do',
    //sortName: 'code',
    //sortOrder: 'desc',
    remoteSort : false,
    loadMsg:'数据正在加载,请等待......',
    idField : 'id',
    singleSelect : true,//是否单选
    pagination : true,//分页控件
    rownumbers : true,//行号
    checkOnSelect : true,
    selectOnCheck : true,
    // remoteSort : false,
    // sortName:'userName',
    // sortOrder:'asc',
    rowStyler:function(index,record){
    console.info(index);
    console.info(record);
    /*if(record.age > 5000){
    return "background:red";
    }*/
    },
    frozenColumns:
    [[
    //你把 ck 换成 QID 试试
    { field: 'QID', checkbox: true }
    ]],
    columns : [ [ {
    field : 'id',
    title : 'id',
    width : '30%'
    }, {
    field : 'userName',
    title : 'userName',
    width : '30%',
    sortable:true
    }, {
    field : 'age',
    title : 'age',
    width : '30%',
    align : 'right',
    sortable:true
    } ] ],
    toolbar : [ {
    text : '添加',
    iconCls : 'icon-add',
    handler : function() {
    flag = 'add';
    $("#my_dialog").dialog({
    title:"新增用户"
    })
    $("#form_user").get(0).reset();
    $("#my_dialog").dialog('open');
    }
    }, '-', {
    text : '修改',
    iconCls : 'icon-edit',
    handler : function() {
    flag = 'edit';
    var arr = $("#tab").datagrid("getSelections");
    if(arr.length != 1){
    $.messager.show({
    title:'提示信息',
    msg:'只能选择一行记录进行修改'
    });
    }else{
    $("#my_dialog").dialog({
    title:"修改用户"
    })
    $("#my_dialog").dialog('open');
    $("#form_user").get(0).reset();
    $("#form_user").form('load',{
    id :arr[0].id ,
    userName : arr[0].userName ,
    age : arr[0].age
    });

    }
    }
    }, '-', {
    text : '删除',
    iconCls : 'icon-remove',
    handler : function() {
    var arr = $("#tab").datagrid("getSelections");
    if(arr.length <= 0){
    $.messager.show({
    title:'提示信息',
    msg:'只能选择一行记录进行删除!'
    });
    }else{
    $.messager.confirm("提示信息","确认删除吗",function(r){
    if(r){
    var ids = '';
    for(var i =0;i<arr.length;i++){
    ids += arr[i].id + ",";
    }
    ids = ids.substring(0,ids.length-1);
    $.post('/egoTest/user/deleteUser.do',{ids:ids},function(result){
    $("#tab").datagrid("reload");
    $("#tab").datagrid('unselectAll'); //清空idFiled
    $.messager.show({
    title:'提示信息',
    msg:'操作成功!'
    });
    });
    }else{
    return;
    }
    })
    }
    }
    } , '-', {
    text : '查询',
    iconCls : 'icon-search',
    handler : function() {
    $("#lay").layout('expand','north');
    }
    }],
    });

    //设置分页控件
    var p = $('#tab').datagrid('getPager');
    $(p).pagination({
    pageSize : 10,//每页显示的记录条数,默认为10
    pageList : [ 5, 10, 15, 20, 50 ],//可以设置每页记录条数的列表
    beforePageText : '第',//页数文本框前显示的汉字
    afterPageText : '页 共 {pages} 页',
    displayMsg : '当前显示 {from} - {to} 条记录 共 {total} 条记录',
    onBeforeRefresh : function() {
    $(this).pagination('loading');
    // alert('before refresh');
    $(this).pagination('loaded');
    $('#tab').datagrid('reload');
    }
    });

    $("#btn1").click(function(){
    if($('#form_user').form('validate')){
    $.ajax({
    type : 'POST',
    url : flag == 'add'?'/egoTest/user/saveUser.do':'/egoTest/user/updateUser.do',
    data: $('#form_user').serialize(),
    dataType : 'json',
    success : function(data) {
    //关闭窗口
    $("#my_dialog").dialog('close');
    //刷新datagrid
    $("#tab").datagrid('reload');
    //提示信息
    $.messager.show({
    title: data.status,
    msg: data.message
    });
    },
    error : function(data) {
    $.messager.show({
    title: '提示信息',
    msg:'错误返回'
    })
    }
    });
    }else{
    $.messager.show({
    title : '提示信息',
    msg:'数据验证不通过,不能保存!'
    })
    }
    });

    $("#btn2").click(function(){
    $("#my_dialog").dialog('close');
    });

    $("#searchbtn").click(function(){
    $("#tab").datagrid('load',serializeForm($("#mysearch")));
    });

    $("#clearbtn").click(function(){
    $("#mysearch").form('clear');
    $("#tab").datagrid('load',{});
    });

    function serializeForm(form){
    var obj = {};
    $.each(form.serializeArray(),function(index){
    if(obj[this['name']]){
    obj[this['name']] = obj[this['name']] + ',' +this['value'];
    }else{
    obj[this['name']] = this['value'];
    }
    });
    return obj;
    }

    });
    </script>
    <style type="text/css">
    body {
    font-family:verdana,helvetica,arial,sans-serif;
    padding:5px;
    font-size:12px;
    margin:0;
    }
    </style>
    </head>
    <body>

    <div id="lay" class="easyui-layout" style=" 100%; height: 100%;">
    <div region="north" title="用户查询" collapsed=true style="height: 100px;">
    <form id="mysearch" method="post">
    用户名:<input name="userName" class="easyui-validatebox" required="true" missingMessage="请输入姓名" value=""/>
    年龄:<input name="age" class="easyui-validatebox" required="true" missingMessage="请输入年龄" value=""/>
    <a id="searchbtn" class="easyui-linkbutton">查询</a>
    <a id="clearbtn" class="easyui-linkbutton">清空</a>
    </form>
    </div>
    <div region="center">
    <table id="tab"></table>
    </div>
    </div>

    <div id="my_dialog" title="新增用户" modal=true draggable=false class="easyui-dialog" closed=true style="700px;height:400px;">
    <form id="form_user" action="">
    <table align="center">
    <tr>
    <td>编号:</td>
    <td><input type="text" id="id" name="id"
    class="easyui-validatebox" required="true" missingMessage="请输入编号" /></td>
    </tr>
    <tr>
    <td>姓名:</td>
    <td><input type="text" id="userName" name="userName"
    class="easyui-validatebox" required="true" missingMessage="请输入姓名" /></td>
    </tr>
    <tr>
    <td>年龄:</td>
    <td><input type="password" id="age" name="age"
    class="easyui-validatebox" required="true" missingMessage="请输入年龄" /></td>
    </tr>
    <tr>
    <td colspan="2">
    <a id="btn1" class="easyui-linkbutton">确定</a>
    <a id="btn2" class="easyui-linkbutton">取消</a>
    </td>
    </tr>

    </table>
    </form>
    </div>
    </body>

    </html>

    2.UserController.java

    package com.controller;

    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONObject;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;

    import com.model.User;
    import com.service.UserService;
    import com.util.Page;

    @Controller
    @RequestMapping("/user")
    public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/getAllUser")
    public String getAllUser(HttpServletRequest request,
    HttpServletResponse response, ModelMap modelMap, Page page) {
    List<String> list = new ArrayList<String>();
    list.add("cxx1");
    list.add("cxx2");
    list.add("cxx3");

    List<User> uList = userService.getAllUser(page);
    modelMap.put("uList", uList);
    modelMap.put("list", list);
    return "index2";

    }

    @RequestMapping("/getAjaxUser")
    public void getAjaxUser(HttpServletRequest request,
    HttpServletResponse response, ModelMap modelMap, Page page) {
    try {
    response.setContentType("text/html;charset=UTF-8");
    List<User> uList = userService.getAllUser(page);
    Map<String, Object> map = new HashMap<String, Object>();
    // map.put( "total",total);
    map.put("total", 57);
    map.put("rows", uList);

    JSONObject json = JSONObject.fromObject(map);
    // String str = "aabbcc";
    PrintWriter pw = response.getWriter();
    pw.write(json.toString());
    pw.flush();
    pw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @RequestMapping("/getAjaxUser2")
    public void getAjaxUser2(HttpServletRequest request,
    HttpServletResponse response, ModelMap modelMap, Page page) {
    try {
    response.setContentType("text/html;charset=UTF-8");
    List<User> uList = userService.getAllUser(page);
    Map<String, Object> map = new HashMap<String, Object>();
    // map.put( "total",total);
    map.put("total", Integer.valueOf(page.getTotal()));
    map.put("rows", uList);

    JSONObject json = JSONObject.fromObject(map);
    // String str = "aabbcc";
    PrintWriter pw = response.getWriter();
    pw.write(json.toString());
    pw.flush();
    pw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @RequestMapping("/queryUser")
    public void queryUser(HttpServletRequest request,
    HttpServletResponse response, ModelMap modelMap, Page page) {
    try {
    String currentPage = request.getParameter("page");
    String pageSize = request.getParameter("rows");

    String userName = request.getParameter("userName")==null?"":request.getParameter("userName");
    String age = request.getParameter("age")==null?"":request.getParameter("age");
    Map<String,Object> queryMap = new HashMap<String,Object>();
    queryMap.put("userName",userName);
    queryMap.put("age",age);

    page.setCurrentPage(Integer.parseInt(currentPage));
    page.setPageSize(Integer.parseInt(pageSize));
    response.setContentType("text/html;charset=UTF-8");
    // Thread.sleep(500);
    List<User> uList = userService.queryUser(page,queryMap);
    Map<String, Object> map = new HashMap<String, Object>();
    // map.put( "total",total);
    map.put("total", Integer.valueOf(page.getTotal()));
    map.put("rows", uList);

    JSONObject json = JSONObject.fromObject(map);
    // String str = "aabbcc";
    PrintWriter pw = response.getWriter();
    pw.write(json.toString());
    pw.flush();
    pw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @RequestMapping("/saveUser")
    public void saveUser(HttpServletRequest request,
    HttpServletResponse response,User u) {

    try{

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    String id = request.getParameter("id");
    String userName = request.getParameter("userName");
    String age = request.getParameter("age");
    u.setId(Integer.parseInt(id));
    u.setUserName(userName);
    u.setAge(Integer.parseInt(age));
    userService.saveUser(u);

    String result = "{"status":"ok","message":"操作成功"}";
    pw.write(result);
    pw.flush();
    pw.close();
    }catch (Exception e) {
    e.printStackTrace();
    }
    }


    @RequestMapping("/updateUser")
    public void updateUser(HttpServletRequest request,
    HttpServletResponse response,User u){
    try{
    String id = request.getParameter("id");
    String userName = request.getParameter("userName");
    String age = request.getParameter("age");
    u = userService.findUserById(id);

    u.setId(Integer.parseInt(id));
    u.setUserName(userName);
    u.setAge(Integer.parseInt(age));
    userService.updateUser(u);

    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter pw = response.getWriter();
    String result = "{"status":"ok","message":"操作成功"}";
    pw.write(result);
    pw.flush();
    pw.close();
    }catch (Exception e) {
    e.printStackTrace();
    }
    }


    @RequestMapping("/deleteUser")
    public void deleteUser(HttpServletRequest request,
    HttpServletResponse response,User u){
    String[] ids = request.getParameter("ids").split(",");
    for(int i=0;i<ids.length;i++){
    userService.deleteUser(ids[i]);
    }
    }


    }



  • 相关阅读:
    oracle sql 优化大全
    MyBatis学习笔记
    Eclipse启动项目时,删除workspaces无用的工作区间
    java 中 BigDecimal 怎么与 0 比较
    Mybatis 异常: The content of elements must consist of well-formed character data or markup
    ODS与数据仓库
    MyBatis 缓存
    管理信息系统需求调研分析指南
    Merge Into 语句代替Insert/Update在Oracle中的应用实战
    Oracle数据库常用函数使用--持续更新中
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/4888869.html
Copyright © 2020-2023  润新知