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.

  1. <table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
  2. url="get_users.php"
  3. toolbar="#toolbar"
  4. rownumbers="true" fitColumns="true" singleSelect="true">
  5. <thead>
  6. <tr>
  7. <th field="firstname" width="50">First Name</th>
  8. <th field="lastname" width="50">Last Name</th>
  9. <th field="phone" width="50">Phone</th>
  10. <th field="email" width="50">Email</th>
  11. </tr>
  12. </thead>
  13. </table>
  14. <div id="toolbar">
  15. <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
  16. <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
  17. <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
  18. </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

  1. $rs = mysql_query('select * from users');
  2. $result = array();
  3. while($row = mysql_fetch_object($rs)){
  4. array_push($result, $row);
  5. }
  6. echo json_encode($result);

Step 3: Create form dialog

To create or edit a user, we use the same dialog.

  1. <div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
  2. closed="true" buttons="#dlg-buttons">
  3. <div class="ftitle">User Information</div>
  4. <form id="fm" method="post" novalidate>
  5. <div class="fitem">
  6. <label>First Name:</label>
  7. <input name="firstname" class="easyui-textbox" required="true">
  8. </div>
  9. <div class="fitem">
  10. <label>Last Name:</label>
  11. <input name="lastname" class="easyui-textbox" required="true">
  12. </div>
  13. <div class="fitem">
  14. <label>Phone:</label>
  15. <input name="phone" class="easyui-textbox">
  16. </div>
  17. <div class="fitem">
  18. <label>Email:</label>
  19. <input name="email" class="easyui-textbox" validType="email">
  20. </div>
  21. </form>
  22. </div>
  23. <div id="dlg-buttons">
  24. <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
  25. <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
  26. </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.

  1. function newUser(){
  2. $('#dlg').dialog('open').dialog('setTitle','New User');
  3. $('#fm').form('clear');
  4. url = 'save_user.php';
  5. }

When edit a user, we open the dialog and load form data from the selected datagrid row.

  1. var row = $('#dg').datagrid('getSelected');
  2. if (row){
  3. $('#dlg').dialog('open').dialog('setTitle','Edit User');
  4. $('#fm').form('load',row);
  5. url = 'update_user.php?id='+row.id;
  6. }

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:

  1. function saveUser(){
  2. $('#fm').form('submit',{
  3. url: url,
  4. onSubmit: function(){
  5. return $(this).form('validate');
  6. },
  7. success: function(result){
  8. var result = eval('('+result+')');
  9. if (result.errorMsg){
  10. $.messager.show({
  11. title: 'Error',
  12. msg: result.errorMsg
  13. });
  14. } else {
  15. $('#dlg').dialog('close'); // close the dialog
  16. $('#dg').datagrid('reload'); // reload the user data
  17. }
  18. }
  19. });
  20. }

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:

  1. function destroyUser(){
  2. var row = $('#dg').datagrid('getSelected');
  3. if (row){
  4. $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
  5. if (r){
  6. $.post('destroy_user.php',{id:row.id},function(result){
  7. if (result.success){
  8. $('#dg').datagrid('reload'); // reload the user data
  9. } else {
  10. $.messager.show({ // show error message
  11. title: 'Error',
  12. msg: result.errorMsg
  13. });
  14. }
  15. },'json');
  16. }
  17. });
  18. }
  19. }

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的更多相关文章

  1. 使用Struts2和jQuery EasyUI实现简单CRUD系统(转载汇总)

    使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——aja ...

  2. jQuery EasyUI 应用 – 创建 CRUD 应用(表格)

    jQuery EasyUI 应用 - 创建 CRUD 应用 本节介绍如何创建CRUD应用. CRUD分别是指在做计算处理时的增加(Create).读取查询(Retrieve).更新(Update)和删 ...

  3. 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合

    这部分比較复杂,之前看过自己的同学开发一个选课系统的时候用到了JSON,可是一直不知道有什么用.写东西也没用到.所以没去学他.然后如今以这样的怀着好奇心,这是做什么用的,这是怎么用的.这是怎么结合的心 ...

  4. jQuery EasyUI学习二

    1.   课程介绍 1.  Datagrid组件(掌握) 2.  Dialog.form组件(掌握) 3. Layout.Tabs;(掌握) Datagrid组件 2.1.  部署运行pss启动无错 ...

  5. 使用Jquery+EasyUI 进行框架项目开发案例讲解之五 模块(菜单)管理源码分享

    http://www.cnblogs.com/huyong/p/3454012.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之五  模块(菜单)管理源码分享    在上四篇文章 ...

  6. [jQuery EasyUI系列] 创建增删改查应用

    一.数据收集并妥善管理数据是网络应用共同的必要.CRUD允许我们生产页面列表并编辑数据库记录. 本文主要演示如何使用jQuery EasyUI实现CRUD DataGrid. 将使用到的插件有: da ...

  7. jQuery EasyUI教程之datagrid应用(一)

    最近一段时间都在做人事系统的项目,主要用到了EasyUI,数据库操作,然后抽点时间整理一下EasyUI的内容. 这里我们就以一个简洁的电话簿软件为基础,具体地说一下datagrid应用吧 datagr ...

  8. Jquery EasyUI DataGrid .net实例

    前台界面:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  9. jquery easyui常见问题:

    1.jquery easyui1.4.2 demo在ie10 上加载json的时候没有效果 从官网上下载了jquery easyui1.4.2 里面有个demo文件夹,但是发现底下的demo在IE.3 ...

随机推荐

  1. Luogu P2577 [ZJOI2005]午餐

    一道贪心+类背包DP的好题 首先发现一个十分显然的性质,没有这个性质整道题目都难以下手: 无论两队的顺序如何,总是让吃饭慢的人先排队 这是一个很显然的贪心,因为如果让吃饭慢的排在后面要更多的时间至少没 ...

  2. SQLAlchemy 关联表删除实验

    本实验所用代码来源于官网文档 from sqlalchemy import Table, Column, Integer, String, ForeignKey from sqlalchemy.orm ...

  3. 编译安装php时遇到virtual memory exhausted: Cannot allocate memory

    有时候用vps建站时需要通过编译的方式来安装主机控制面板.对于大内存的VPS来说一般问题不大,但是对于小内存,比如512MB内存的godaddy VPS来说,很有可能会出现问题,因为编译过程是一个内存 ...

  4. Windows下的Anaconda+OpenCV的环境配置

    Windows下的Anaconda+OpenCV的环境配置

  5. redis安装启动和数据操作

    redis安装和启动 1.安装包下载地址 >> redis基本数据类型 string(字符串和数值) .list(列表/队列).hashmap(哈希表[键唯一]). set(集合[值唯一] ...

  6. Android与单片机通信常用数据转换方法(汇总)

    下面直接贴代码 1.  将GB2312转化为中文,如BAFAC2DCB2B7→胡萝卜,两个字节合成一个文字 public static String stringToGbk(String string ...

  7. webpack 支持的模块方法

    在webpack中支持的模块语法风格有:ES6,commonJS和AMD ES6风格(推荐) 在webpack2中,webpack支持ES6模块语法.这意味着在没有babel等工具处理的情况下你就可以 ...

  8. Revit二次开发-根据视图阶段(Phase)创建房间

    最近开发业务中,有一个自动创建房间的功能,很自然的想到了Document.NewRooms2方法.但是当前功能的特殊之处在于,Revit项目视图是分阶段(Phase)的,不同阶段的房间是互相独立的. ...

  9. Grin v0.5在Ubuntu下的安装和启动

    Grin和bitcoin一样也是一种点对点的现金交易系统,但它通过零和验证算法,使得双方的交易金额不会被第三方知晓,让它在隐私保护方面更强.其官方的介绍是: 所有人的电子交易,没有审查或限制.并提出它 ...

  10. App云测试服务对比

    前言: 我们都知道在测试移动app时最耗时的是在各种测试设备进行测试, 因为不论是安卓还是iOS都已经碎片化了.而云测试看似是解决这一问题的有效途径.因此选择哪种云测试平台来协助测试人员进行各种测试就 ...