Build CRUD Application with jQuery EasyUI
http://www.jeasyui.com/tutorial/app/crud.php
It has become a common necessily for web application to collect data and manage it properly. CRUD allows us to generate pages to list and edit database records. This tutorial will show you how to implement a CRUD DataGrid using jQuery EasyUI framework.
We will use following plugins:
- datagrid: show the user list data.
 - dialog: create or edit a single user information.
 - form: used to submit form data.
 - messager: to show some operation messages.
 
Step 1: Prepare database
We will use MySql database to store user information. Create database and 'users' table.

Step 2: Create DataGrid to display user information
Create the DataGrid with no javascript code.
- <table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
 - url="get_users.php"
 - toolbar="#toolbar"
 - rownumbers="true" fitColumns="true" singleSelect="true">
 - <thead>
 - <tr>
 - <th field="firstname" width="50">First Name</th>
 - <th field="lastname" width="50">Last Name</th>
 - <th field="phone" width="50">Phone</th>
 - <th field="email" width="50">Email</th>
 - </tr>
 - </thead>
 - </table>
 - <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>
 
We don't need to write any javascript code and we can show the user list as following image:

The DataGrid use the 'url' property that is assigned to 'get_users.php' to retrieve data from server.
Code of get_users.php file
- $rs = mysql_query('select * from users');
 - $result = array();
 - while($row = mysql_fetch_object($rs)){
 - array_push($result, $row);
 - }
 - echo json_encode($result);
 
Step 3: Create form dialog
To create or edit a user, we use the same dialog.
- <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" novalidate>
 - <div class="fitem">
 - <label>First Name:</label>
 - <input name="firstname" class="easyui-textbox" required="true">
 - </div>
 - <div class="fitem">
 - <label>Last Name:</label>
 - <input name="lastname" class="easyui-textbox" required="true">
 - </div>
 - <div class="fitem">
 - <label>Phone:</label>
 - <input name="phone" class="easyui-textbox">
 - </div>
 - <div class="fitem">
 - <label>Email:</label>
 - <input name="email" class="easyui-textbox" validType="email">
 - </div>
 - </form>
 - </div>
 - <div id="dlg-buttons">
 - <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
 - <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
 - </div>
 
The dialog is created with no javascript code also.

Step 4: Implement creating and editing a user
When create a user, we open the dialog and clear form data.
- function newUser(){
 - $('#dlg').dialog('open').dialog('setTitle','New User');
 - $('#fm').form('clear');
 - url = 'save_user.php';
 - }
 
When edit a user, we open the dialog and load form data from the selected datagrid row.
- var row = $('#dg').datagrid('getSelected');
 - if (row){
 - $('#dlg').dialog('open').dialog('setTitle','Edit User');
 - $('#fm').form('load',row);
 - url = 'update_user.php?id='+row.id;
 - }
 
The 'url' stores the URL address where the form will post to when save the user data.
Step 5: Save the user data
To save the user data we use the following code:
- 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'); // close the dialog
 - $('#dg').datagrid('reload'); // reload the user data
 - }
 - }
 - });
 - }
 
Before submit the form, the 'onSubmit' function will be called, in which we can validate the form field value. When the form field values are submited successfully, close the dialog and reload the datagrid data.
Step 6: Remove a user
To remove a user, we use the following code:
- 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){
 - $.post('destroy_user.php',{id:row.id},function(result){
 - if (result.success){
 - $('#dg').datagrid('reload'); // reload the user data
 - } else {
 - $.messager.show({ // show error message
 - title: 'Error',
 - msg: result.errorMsg
 - });
 - }
 - },'json');
 - }
 - });
 - }
 - }
 

Before remove a row, we will display a confirm dialog to let user to determine whether to really remove the row data. When remove data successfully, call 'reload' method to refresh the datagrid data.
Step 7: Run the Code
Run the code in your browser with MySQL started.
So, you now know the basics of CRUD in jQuery EasyUI framework. Press below link to start the demo application.
Download the EasyUI example:
Build CRUD Application with jQuery EasyUI的更多相关文章
- 使用Struts2和jQuery EasyUI实现简单CRUD系统(转载汇总)
		
使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——aja ...
 - jQuery EasyUI 应用 – 创建 CRUD 应用(表格)
		
jQuery EasyUI 应用 - 创建 CRUD 应用 本节介绍如何创建CRUD应用. CRUD分别是指在做计算处理时的增加(Create).读取查询(Retrieve).更新(Update)和删 ...
 - 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合
		
这部分比較复杂,之前看过自己的同学开发一个选课系统的时候用到了JSON,可是一直不知道有什么用.写东西也没用到.所以没去学他.然后如今以这样的怀着好奇心,这是做什么用的,这是怎么用的.这是怎么结合的心 ...
 - jQuery EasyUI学习二
		
1. 课程介绍 1. Datagrid组件(掌握) 2. Dialog.form组件(掌握) 3. Layout.Tabs;(掌握) Datagrid组件 2.1. 部署运行pss启动无错 ...
 - 使用Jquery+EasyUI 进行框架项目开发案例讲解之五   模块(菜单)管理源码分享
		
http://www.cnblogs.com/huyong/p/3454012.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之五 模块(菜单)管理源码分享 在上四篇文章 ...
 - [jQuery EasyUI系列] 创建增删改查应用
		
一.数据收集并妥善管理数据是网络应用共同的必要.CRUD允许我们生产页面列表并编辑数据库记录. 本文主要演示如何使用jQuery EasyUI实现CRUD DataGrid. 将使用到的插件有: da ...
 - jQuery EasyUI教程之datagrid应用(一)
		
最近一段时间都在做人事系统的项目,主要用到了EasyUI,数据库操作,然后抽点时间整理一下EasyUI的内容. 这里我们就以一个简洁的电话簿软件为基础,具体地说一下datagrid应用吧 datagr ...
 - Jquery EasyUI DataGrid .net实例
		
前台界面:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
 - jquery easyui常见问题:
		
1.jquery easyui1.4.2 demo在ie10 上加载json的时候没有效果 从官网上下载了jquery easyui1.4.2 里面有个demo文件夹,但是发现底下的demo在IE.3 ...
 
随机推荐
- 20155234 《网络对抗》Exp 8 Web基础
			
基础问答 什么是表单 可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单包括两个部分:一部分是HTML源代码用于描述表单(例如,域,标签和用户在页面上看见的按钮),另一部分是脚本 ...
 - vue 监听页面宽度变化 和 键盘事件
			
vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { scr ...
 - R绘图 第十篇:绘制文本、注释和主题(ggplot2)
			
使用ggplot2包绘制时,为了更直观地向用户显示报表的内容和外观,需要使用geom_text()函数添加文本说明,使用annotate()添加注释,并通过theme()来调整非数据的外观. 一,文本 ...
 - PowerBI开发 第十四篇:使用M公式添加列
			
PowerBI的查询编辑器使用Power Query M公式语言来定义查询模型,它是一种富有表现力的数据糅合(Mashup)语言,一个M查询可以计算(Evalute)一个表达式,得到一个值. 对于开发 ...
 - stl源码剖析 详细学习笔记 算法(5)
			
//---------------------------15/04/01---------------------------- //inplace_merge(要求有序) template< ...
 - Spring+SpringMVC+MyBatis整合优化篇
			
优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log Spring+SpringMVC+MyBatis+easyUI整合优化篇 ...
 - 记录:Ubuntu 18.04 安装 tensorflow-gpu 版本
			
狠下心来重新装了系统,探索一下 gpu 版本的安装.比较令人可喜的是,跟着前辈们的经验,还是让我给安装成功了.由于我是新装的系统,就像婴儿般纯净,所以进入系统的第一步就是安装 cuda,只要这个不出错 ...
 - 2018-07-09--记录一次gitlab迁移事件及遇到的问题
			
一.事情起因 因机房服务器即将到期,需要将即将到期的服务器迁移至云上,迁移之前没有查看老环境的Gitlab是什么版本,直接装的Gitlab社区版,做数据导入时提示版本错误: [root@vpn-ser ...
 - Unity 图文重现官方教程视频 2droguelike 第一集
			
初衷: 本人初学Unity,四处收集了一些视频和教材,学习和摸索了一段时间, 我发现官网教程简单易上手,只不过他是英文讲解不方便,我就想把他翻译翻译吧, 然后我又发现看视频学习要暂停回放好多遍,麻烦, ...
 - mongodb lock 出毛病时解决方法
			
错误信息: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 解决办法: sudo r ...