jQueryEasyUI应用 – datagrid之CRUD应用
本文 jQueryEasyUI + SpringBoot + Mybatis整合Datagrid的CRUD应用
一、前言准备
1、我们将使用下面的插件:
- datagrid:向用户展示列表数据。
- dialog:创建或编辑一条单一的用户信息。
- form:用于提交表单数据。
- messager:显示一些操作信息。
2、数据库准备
CREATE TABLE `users` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`firstname` varchar(50) DEFAULT NULL COMMENT '姓',
`lastname` varchar(50) DEFAULT NULL COMMENT '名',
`phone` varchar(20) DEFAULT NULL COMMENT '电话',
`email` varchar(40) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8
3、下载jquery-easyui-1.6.3zip包
jquery-easyui-1.6.3包下:
1、themes文件夹放到static目录下
2、jquery.easyui.min.js、jquery.min.js放到static目录下
3、demo.css放到template目录下
4、相关页面放到template目录下
二、搭建项目并开发
1、搭建项目
a)、idea中创建SpringBoot项目
b)、编写pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itcast</groupId>
<artifactId>jquery-ui</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>jquery-ui</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency> <!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency> <!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <!-- pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency> <!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> <!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency> <!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.webjars.npm/jquery-easyui -->
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>jquery-easyui</artifactId>
<version>1.5.21</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.webjars/jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
c)、编写application.yml文件
server:
port: 1111
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.itcast.jqueryui.entities
mapper-locations:
- classpath:mybatis/mapper/**/*.xml spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/jquery_ui
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 10
max-total: 10
max-wait-millis: 200
resources:
static-locations: ["classpath:/static/","classpath:/templates/demo/datagrid","classpath:/","classpath:/templates"] pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
offset-as-page-num: true
row-bounds-with-count: true
d)、mybatis.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings> </configuration>
2、创建数据网格页面
a)、创建basic.html页面,并引入相关的js和css
<head>
<meta charset="UTF-8">
<title>Basic DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../../../static/themes/default/easyui.css" th:href="@{../../../static/themes/default/easyui.css}">
<link rel="stylesheet" type="text/css" href="../../../static/themes/icon.css" th:href="@{../../../static/themes/icon.css}">
<link rel="stylesheet" type="text/css" href="../demo.css" th:href="@{../demo.css}">
<script type="text/javascript" src="../../../static/jquery.min.js" th:href="@{../../../static/jquery.min.js}"></script>
<script type="text/javascript" src="../../../static/jquery.easyui.min.js" th:href="@{../../../static/jquery.easyui.min.js}"></script>
</head>
b)、创建数据列表和工具按钮
<h2>Basic DataGrid</h2>
<p>The DataGrid is created from markup, no JavaScript code needed.</p>
<div style="margin:20px 0;"></div> <!-- 数据列表 start -->
<table id="dg" title="My Users" class="easyui-datagrid" style="width:580px;height:250px"
url="/datagrid/list" toolbar="#toolbar" singleSelect="true" method="get">
<thead>
<tr>
<th field="id" width="60" align="center">ID</th>
<th field="firstname" width="100" align="center">First Name</th>
<th field="lastname" width="100" align="center">Last Name</th>
<th field="phone" width="160" align="center">Phone</th>
<th field="email" width="160" align="center">Email</th>
</tr>
</thead>
</table>
<!-- 数据列表 end --> <!-- 工具按钮 start -->
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
</div>
<!-- 工具按钮 end -->
c)、java后台页面
实体类:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: Datagrid
* Author: JohnEricCheng
* Date: 2018/9/25 15:13
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.entities; import java.io.Serializable; public class Datagrid implements Serializable{ private Integer id;
private String firstname;
private String lastname;
private String phone;
private String email; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getFirstname() {
return firstname;
} public void setFirstname(String firstname) {
this.firstname = firstname;
} public String getLastname() {
return lastname;
} public void setLastname(String lastname) {
this.lastname = lastname;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "Datagrid{" +
"id=" + id +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
'}';
}
}
Controller层:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridController
* Author: JohnEricCheng
* Date: 2018/9/25 14:56
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.controller; import com.alibaba.fastjson.JSON;
import com.itcast.jqueryui.entities.Datagrid;
import com.itcast.jqueryui.service.DatagridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
@RequestMapping("/datagrid")
public class DatagridController { @Autowired
private DatagridService datagridService; /**
* 获取所有用户列表
*
* @return
*/
@RequestMapping("/list")
@ResponseBody
public List<Datagrid> list() {
List<Datagrid> datagridList = datagridService.list();
return datagridList;
}
}
service层:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridService
* Author: JohnEricCheng
* Date: 2018/9/25 15:16
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.service; import com.itcast.jqueryui.entities.Datagrid;
import org.springframework.transaction.annotation.Transactional; import java.util.List; public interface DatagridService { public List<Datagrid> list(); @Transactional
public boolean saveOrUpdate(Datagrid datagrid); @Transactional
public boolean delete(Integer id);
}
ServiceImpl层:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridServiceImpl
* Author: JohnEricCheng
* Date: 2018/9/25 15:17
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.service.impl; import com.itcast.jqueryui.dao.DatagridDao;
import com.itcast.jqueryui.entities.Datagrid;
import com.itcast.jqueryui.service.DatagridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DatagridServiceImpl implements DatagridService { @Autowired
private DatagridDao datagridDao; @Override
public List<Datagrid> list() {
return datagridDao.findAll();
} @Override
public boolean saveOrUpdate(Datagrid datagrid) {
return datagridDao.saveOrUpdate(datagrid);
} @Override
public boolean delete(Integer id) {
return datagridDao.delete(id);
}
}
Dao层:
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridDao
* Author: JohnEricCheng
* Date: 2018/9/25 15:22
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.dao; import com.itcast.jqueryui.entities.Datagrid;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface DatagridDao { public List<Datagrid> findAll(); public boolean saveOrUpdate(Datagrid datagrid); public boolean delete(Integer id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itcast.jqueryui.dao.DatagridDao">
<select id="findAll" resultType="Datagrid">
select id,firstname,lastname,phone,email from users;
</select>
</mapper>
d)、页面展示结果
3、新增编辑用户
a)、前台页面代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Basic DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../../../static/themes/default/easyui.css" th:href="@{../../../static/themes/default/easyui.css}">
<link rel="stylesheet" type="text/css" href="../../../static/themes/icon.css" th:href="@{../../../static/themes/icon.css}">
<link rel="stylesheet" type="text/css" href="../demo.css" th:href="@{../demo.css}">
<script type="text/javascript" src="../../../static/jquery.min.js" th:href="@{../../../static/jquery.min.js}"></script>
<script type="text/javascript" src="../../../static/jquery.easyui.min.js" th:href="@{../../../static/jquery.easyui.min.js}"></script>
</head>
<body>
<h2>Basic DataGrid</h2>
<p>The DataGrid is created from markup, no JavaScript code needed.</p>
<div style="margin:20px 0;"></div> <!--<table class="easyui-datagrid" title="Basic DataGrid" style="width:580px;height:250px"
data-options="singleSelect:true,collapsible:true,url:'/datagrid/list',method:'get'">-->
<!-- 数据列表 start -->
<table id="dg" title="My Users" class="easyui-datagrid" style="width:580px;height:250px"
url="/datagrid/list" toolbar="#toolbar" singleSelect="true" method="get">
<thead>
<tr>
<th field="id" width="60" align="center">ID</th>
<th field="firstname" width="100" align="center">First Name</th>
<th field="lastname" width="100" align="center">Last Name</th>
<th field="phone" width="160" align="center">Phone</th>
<th field="email" width="160" align="center">Email</th>
</tr>
</thead>
</table>
<!-- 数据列表 end --> <!-- 工具按钮 start -->
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
</div>
<!-- 工具按钮 end --> <!-- 新增(form表单) start -->
<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">User Information</div>
<form id="fm" method="post">
<div class="fitem">
<label>First Name:</label>
<input type="hidden" name="id">
</div> <div class="fitem">
<label>First Name:</label>
<input name="firstname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Last Name:</label>
<input name="lastname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Phone:</label>
<input name="phone">
</div>
<div class="fitem">
<label>Email:</label>
<input name="email" class="easyui-validatebox" validType="email">
</div>
</form>
</div>
<!-- 新增(form表单) end --> <!-- 新增中保存和取消按钮 start -->
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>
<!-- 新增中保存和取消按钮 end -->
</body> <script type="text/javascript">
<!-- 新增用户窗口 -->
function newUser() {
$("#dlg").dialog("open").dialog('setTitle','New User');
$("#fm").form("clear");
url = "/datagrid/saveOrUpdate";
}
<!-- 编辑用户窗口 -->
function editUser(){
var row = $("#dg").datagrid("getSelected");
if(row) {
$("#dlg").dialog("open").dialog("setTitle","Edit User");
$("#dlg").form("load",row);
url = "/datagrid/saveOrUpdate?id="+row.id;
}
} <!-- 保存用户 -->
function saveUser() {
$("#fm").form('submit',{
url: url,
method:"post",
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'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
},
});
} </script>
b)、后台交互代码
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridController
* Author: JohnEricCheng
* Date: 2018/9/25 14:56
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.controller; import com.alibaba.fastjson.JSON;
import com.itcast.jqueryui.entities.Datagrid;
import com.itcast.jqueryui.service.DatagridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
@RequestMapping("/datagrid")
public class DatagridController { @Autowired
private DatagridService datagridService; /**
* 获取所有用户列表
*
* @return
*/
@RequestMapping("/list")
@ResponseBody
public List<Datagrid> list() {
List<Datagrid> datagridList = datagridService.list();
return datagridList;
} /**
* 保存或修改用户
*
* @param id
* @param firstname
* @param lastname
* @param phone
* @param email
* @return
*/
@PostMapping("/saveOrUpdate")
@ResponseBody
public boolean saveOrUpdate(@RequestParam Integer id, @RequestParam String firstname, @RequestParam String lastname, @RequestParam String phone, @RequestParam String email) {
Datagrid datagrid = new Datagrid();
datagrid.setId(id);
datagrid.setFirstname(firstname);
datagrid.setLastname(lastname);
datagrid.setPhone(phone);
datagrid.setEmail(email);
return datagridService.saveOrUpdate(datagrid);
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridService
* Author: JohnEricCheng
* Date: 2018/9/25 15:16
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.service; import com.itcast.jqueryui.entities.Datagrid;
import org.springframework.transaction.annotation.Transactional; import java.util.List; public interface DatagridService { public List<Datagrid> list(); @Transactional
public boolean saveOrUpdate(Datagrid datagrid); @Transactional
public boolean delete(Integer id);
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridServiceImpl
* Author: JohnEricCheng
* Date: 2018/9/25 15:17
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.service.impl; import com.itcast.jqueryui.dao.DatagridDao;
import com.itcast.jqueryui.entities.Datagrid;
import com.itcast.jqueryui.service.DatagridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DatagridServiceImpl implements DatagridService { @Autowired
private DatagridDao datagridDao; @Override
public List<Datagrid> list() {
return datagridDao.findAll();
} @Override
public boolean saveOrUpdate(Datagrid datagrid) {
return datagridDao.saveOrUpdate(datagrid);
} @Override
public boolean delete(Integer id) {
return datagridDao.delete(id);
}
}
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: DatagridDao
* Author: JohnEricCheng
* Date: 2018/9/25 15:22
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.itcast.jqueryui.dao; import com.itcast.jqueryui.entities.Datagrid;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface DatagridDao { public List<Datagrid> findAll(); public boolean saveOrUpdate(Datagrid datagrid); public boolean delete(Integer id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itcast.jqueryui.dao.DatagridDao">
<select id="findAll" resultType="Datagrid">
select id,firstname,lastname,phone,email from users;
</select> <insert id="saveOrUpdate" >
<!-- <selectKey keyProperty="id" resultType="int" order="BEFORE">
select count(*) from users where id = #{id}
</selectKey>-->
<if test="id != null">
update users
set firstname = #{firstname},lastname = #{lastname},phone = #{phone},email = #{email}
where id = #{id}
</if>
<if test="id==null">
insert into users values(#{id},#{firstname},#{lastname},#{phone},#{email})
</if>
</insert>
</mapper>
d)、页面显示
3、删除用户
a)、前台代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Basic DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../../../static/themes/default/easyui.css" th:href="@{../../../static/themes/default/easyui.css}">
<link rel="stylesheet" type="text/css" href="../../../static/themes/icon.css" th:href="@{../../../static/themes/icon.css}">
<link rel="stylesheet" type="text/css" href="../demo.css" th:href="@{../demo.css}">
<script type="text/javascript" src="../../../static/jquery.min.js" th:href="@{../../../static/jquery.min.js}"></script>
<script type="text/javascript" src="../../../static/jquery.easyui.min.js" th:href="@{../../../static/jquery.easyui.min.js}"></script>
</head>
<body>
<h2>Basic DataGrid</h2>
<p>The DataGrid is created from markup, no JavaScript code needed.</p>
<div style="margin:20px 0;"></div> <!--<table class="easyui-datagrid" title="Basic DataGrid" style="width:580px;height:250px"
data-options="singleSelect:true,collapsible:true,url:'/datagrid/list',method:'get'">-->
<!-- 数据列表 start -->
<table id="dg" title="My Users" class="easyui-datagrid" style="width:580px;height:250px"
url="/datagrid/list" toolbar="#toolbar" singleSelect="true" method="get">
<thead>
<tr>
<th field="id" width="60" align="center">ID</th>
<th field="firstname" width="100" align="center">First Name</th>
<th field="lastname" width="100" align="center">Last Name</th>
<th field="phone" width="160" align="center">Phone</th>
<th field="email" width="160" align="center">Email</th>
</tr>
</thead>
</table>
<!-- 数据列表 end --> <!-- 工具按钮 start -->
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
</div>
<!-- 工具按钮 end --> <!-- 新增(form表单) start -->
<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
closed="true" buttons="#dlg-buttons">
<div class="ftitle">User Information</div>
<form id="fm" method="post">
<div class="fitem">
<label>First Name:</label>
<input type="hidden" name="id">
</div> <div class="fitem">
<label>First Name:</label>
<input name="firstname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Last Name:</label>
<input name="lastname" class="easyui-validatebox" required="true">
</div>
<div class="fitem">
<label>Phone:</label>
<input name="phone">
</div>
<div class="fitem">
<label>Email:</label>
<input name="email" class="easyui-validatebox" validType="email">
</div>
</form>
</div>
<!-- 新增(form表单) end --> <!-- 新增中保存和取消按钮 start -->
<div id="dlg-buttons">
<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>
<!-- 新增中保存和取消按钮 end -->
</body> <script type="text/javascript">
<!-- 新增用户窗口 -->
function newUser() {
$("#dlg").dialog("open").dialog('setTitle','New User');
$("#fm").form("clear");
url = "/datagrid/saveOrUpdate";
} <!-- 编辑用户窗口 -->
function editUser(){
var row = $("#dg").datagrid("getSelected");
if(row) {
$("#dlg").dialog("open").dialog("setTitle","Edit User");
$("#dlg").form("load",row);
url = "/datagrid/saveOrUpdate?id="+row.id;
}
} <!-- 保存用户 -->
function saveUser() {
$("#fm").form('submit',{
url: url,
method:"post",
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'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
},
});
} <!-- 删除用户 -->
function destroyUser() {
var row = $("#dg").datagrid("getSelected");
if(row) {
$.messager.confirm("Confirm","Are you sure you want to destroy this user?",function(r) {
if(r) {
var url = "/datagrid/delete?id="+row.id;
$.post(url,{id:row.id},function(result) {
if(result) {
$('#dg').datagrid('reload'); //reload the user data
}else {
$.messager.show({ //show error msg
title: "Error",
msg: result.errorMsg
});
}
},'json');
}
});
}
} </script>
</html>
b)、后台交互代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itcast.jqueryui.dao.DatagridDao">
<select id="findAll" resultType="Datagrid">
select id,firstname,lastname,phone,email from users;
</select> <insert id="saveOrUpdate" >
<!-- <selectKey keyProperty="id" resultType="int" order="BEFORE">
select count(*) from users where id = #{id}
</selectKey>-->
<if test="id != null">
update users
set firstname = #{firstname},lastname = #{lastname},phone = #{phone},email = #{email}
where id = #{id}
</if>
<if test="id==null">
insert into users values(#{id},#{firstname},#{lastname},#{phone},#{email})
</if>
</insert> <delete id="delete" parameterType="integer">
delete from users where id = #{id}
</delete>
</mapper>
c)、页面展示
jQueryEasyUI应用 – datagrid之CRUD应用的更多相关文章
- struts2 easyui实现datagrid的crud
最近两天因为项目需要,接触了easyui,要用它的datagrid实现crud.第一次做,花了一天时间才完成所有功能,昨天做另外一个模块,同样的功能只用了两个小时. 现在把第一次做datagrid时遇 ...
- jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法
jquery-easyui的datagrid在checkbox多选时,行选中不正确应,去除高亮的解决方法 工作中用到一个具有多选功能的easyui-datagrid在处理cell的点击事件时,不同 ...
- JqueryEasyUI之DataGrid扩展
DataGrid通用合并扩展方法: $.extend($.fn.datagrid.methods, { autoMergeCells: function (jq, fields) { return j ...
- jquery-easyui中datagrid扩展,隐藏显示表头功能
今天,后台中需要新增一个功能,用户可以自由选择显示的列,之后保存到本地localStroage中.所以扩展了easyui中datagrid的onHeaderContextMenu方法. 使用方法: _ ...
- 【转】jquery-easyui中datagrid的单击删除此行
最近在easyui的项目开发,easyui封装了许多方法,用起来很方便,但同时也遇到了不少的问题. 如果在datagrid中直接将index传给easyui自带的deletRow方法来删除当前点击行, ...
- jQueryEasyUI中DataGrid的height,width,fit,fitColumns属性
height: 600, //不指定则默认垂直包裹,指定了则固定 width:1200,//不指定则水平100%平铺,指定了则固定 fit:false,//true:高度填充父窗体,忽略height属 ...
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现)
“MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现) 一.前三篇的内容是否对您有帮助呢?如果有的话,请您继续关注这篇吧,这篇主要是实现”用户管理“的 ...
- jQuery EasyUI教程之datagrid应用-1
一.利用jQuery EasyUI的DataGrid创建CRUD应用 对网页应用程序来说,正确采集和管理数据通常很有必要,DataGrid的CRUD功能允许我们创建页面来列表显示和编辑数据库记录.本教 ...
- jQuery EasyUI教程之datagrid应用
一.利用jQuery EasyUI的DataGrid创建CRUD应用 对网页应用程序来说,正确采集和管理数据通常很有必要,DataGrid的CRUD功能允许我们创建页面来列表显示和编辑数据库记录.本教 ...
随机推荐
- java学习之方法内部类
*方法内部类 * 在类中的方法内定义一个类 * 注意: * 1 方法内部类只能在类的方法内实例化不可以在此方法外实例化 * 2 .方法内部类对象只能用所在方法体中的变量不能改,jdk1.8可以 * 以 ...
- 微信公众平台开发教程(一)_微信接入校验以及token获取
微信公众平台是运营者通过公众号为微信用户提供资讯和服务的平台,而公众平台开发接口则是提供服务的基础. 接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 2.验证服务器地址的有效性 ...
- mysql的执行过程
1 总流程 https://www.cnblogs.com/annsshadow/p/5037667.html 2 优化器 https://www.cnblogs.com/olinux/p/5 ...
- 梳理vue双向绑定的实现原理
Vue 采用数据劫持结合发布者-订阅者模式的方式来实现数据的响应式,通过Object.defineProperty来劫持数据的setter,getter,在数据变动时发布消息给订阅者,订阅者收到消息后 ...
- (转载)WinRAR离购买许可只剩x天
在WinRAR的安装目录下,新建文件RarReg.key(C:\Program Files\WinRAR\RarReg.key) 粘贴以下内容即可: RAR registration dataFede ...
- Excel坐标自动在AutoCad绘图_3
众所周知,Excel对数据处理的功能非常强大,它可以进行数据处理.统计分析已经辅助决策的操作,该软件已经渗透到各个领域.作为一个测绘人,GISer, 也经常利用excel完成一些测量表格的自动化计算, ...
- .NetCore技术研究-EntityFramework Core 3.0 Preview
前段时间.Net Core 3.0 发布了,Entity Framework Core 3.0 也发布了Preview版.假期用了一上午大致研究了一遍,同时又体验了一把Visual Studio 20 ...
- Java获取项目根目录等其他系统属性
一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProper ...
- java问题排查之Load CPU很高
1首先 找到对应的java进程id ps -aux | grep java 这个命令可以找到 2.接下来就是查找比较耗CPU的线程id top -H -p pid 这里可以观察出来耗时最多的几个进程中 ...
- Jmeter上传附件EXCEL
1.通过对上传附件接口进行抓包,获取的信息如下: 2.在jmeter脚本中添加http请求,并添加http请求头信息如下: 3.在http请求中添加上传附件的内容如下,由于我上传的是excel,所以M ...