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. 20155235 《网络攻防》 实验九 Web安全基础

    20155235 <网络攻防> 实验九 Web安全基础 实验内容 SQL注入攻击 XSS攻击 CSRF攻击 WebGoat WebGoat是OWASP组织研制出的用于进行web漏洞实验的应 ...

  2. 编程语法分析之“优先级”和“结合律”

    上节<编程语法分析之从表达式说起>中说到表达式,他的主要作用就是返回一个值!那这个值具体是多少,就要看表达式的整个运算过程.要理解表达式的运算过程就必须了解"优先级"和 ...

  3. 洛咕 P3756 [CQOI2017]老C的方块

    四染色,贼好想 一个弃疗图形刚好对应一个红-绿-黄-粉色路线(不要吐槽颜色) 就是裸的最小割,建图傻逼懒得写了 #include<bits/stdc++.h> #define il inl ...

  4. Java关键字 Finally执行与break, continue, return等关键字的关系

    长文短总结: 在程序没有在执行到finally之前异常退出的情况下,finally是一定执行的,即在finally之前的return语句将在finally执行之后执行. finally总是在控制转移语 ...

  5. 能帮我们学习吉他的音乐软件——Guitar Pro

    Guitar Pro是一款十分好用的吉他软件,也是目前广大音乐爱好者最喜欢的多音轨的音谱编辑软件.支持MIDI.MusicXML.PTB.GTP等多种格式文件的导入/导出. Guitar Pro 7. ...

  6. ASP.NET Core采用Web Deploy方式发布到 Windows Server 2012 IIS上

    小白一枚,租个服务器,发布下网站,满足下好奇心,讲的粗糙,请大家谅解哈~ 1.配置服务器环境.这部分网上教程比较多. (1)开启IIS,参考其他博客的,担心的话,将Web服务器(IIS)全选哈~ (2 ...

  7. python图像处理 模式转化简单总结

    图像处理库PIL有九种不同模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F 1.模式“1” 模式“1”为二值图像,非黑即白.但是它每个像素用8个bit表示,0表示黑,255表示白. 2 ...

  8. HyperLedger/Fabric JAVA-SDK with 1.1

    HyperLedger/Fabric JAVA-SDK with 1.1 该项目可直接在github上访问. 该项目介绍如何使用fabric-sdk-java框架,基于fabric-sdk-java ...

  9. 通过监控Nginx日志来实时屏蔽高频恶意访问的IP

    目前在我的VPS上主要通过两种方式来限制ip的访问次数. 通过Nginx的limit_req配置来限制同一ip在一分钟内的访问次数 通过Nginx deny封禁一天内超过指定次数的ip(需要一个监控脚 ...

  10. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...