[转载]Build CRUD DataGrid with jQuery EasyUI - jQuery EasyUI

[转载]Build CRUD DataGrid with jQuery EasyUI – jQuery EasyUI.

In the previous tutorial we create a crud application that using dialog component to create or edit user information. This tutorial will show you how to create a crud datagrid. We will use the editable datagrid plugin to make these crud actions work.

View Demo

Step 1: Define DataGrid in HTML tag

  1. <table id=“dg” title=“My Users” style=“width:550px;height:250px”
  2. toolbar=“#toolbar”
  3. rownumbers=“true” fitColumns=“true” singleSelect=“true”>
  4. <thead>
  5. <tr>
  6. <th field=“firstname” width=“50” editor=“{type:’validatebox’,options:{required:true}}”>First Name</th>
  7. <th field=“lastname” width=“50” editor=“{type:’validatebox’,options:{required:true}}”>Last Name</th>
  8. <th field=“phone” width=“50” editor=“text”>Phone</th>
  9. <th field=“email” width=“50” editor=“{type:’validatebox’,options:{validType:’email’}}”>Email</th>
  10. </tr>
  11. </thead>
  12. </table>
  13. <div id=“toolbar”>
  14. <a href=“#” class=“easyui-linkbutton” iconCls=“icon-add” plain=“true” onclick=JavaScript:$(‘#dg’).edatagrid(‘addRow’)”>New</a>
  15. <a href=“#” class=“easyui-linkbutton” iconCls=“icon-remove” plain=“true” onclick=JavaScript:$(‘#dg’).edatagrid(‘destroyRow’)”>Destroy</a>
  16. <a href=“#” class=“easyui-linkbutton” iconCls=“icon-save” plain=“true” onclick=“javascript:$(‘#dg’).edatagrid(‘saveRow’)”>Save</a>
  17. <a href=“#” class=“easyui-linkbutton” iconCls=“icon-undo” plain=“true” onclick=“javascript:$(‘#dg’).edatagrid(‘cancelRow’)”>Cancel</a>
  18. </div>

Step 2: Make an editable DataGrid

  1. $(‘#dg’).edatagrid({
  2. url: ‘get_users.php’,
  3. saveUrl: ‘save_user.php’,
  4. updateUrl: ‘update_user.php’,
  5. destroyUrl: ‘destroy_user.php’
  6. });

We should provide the ‘url’,’saveUrl’,’updateUrl’ and ‘destroyUrl’ properties for editable datagrid:

  • url: to retrieve user data from server.
  • saveUrl: to save a new user data.
  • updateUrl: to update an exists user data.
  • destroyUrl: to destroy an exists user data.

Step 3: Write server processing code

Save a new user(save_user.php):

  1. $firstname = $_REQUEST[‘firstname’];
  2. $lastname = $_REQUEST[‘lastname’];
  3. $phone = $_REQUEST[‘phone’];
  4. $email = $_REQUEST[’email’];
  5. include ‘conn.php’;
  6. $SQL = “insert into users(firstname,lastname,phone,email) values(‘$firstname’,’$lastname’,’$phone’,’$email’)”;
  7. @mySQL_query($SQL);
  8. echo json_encode(array(
  9. ‘id’ => mysql_insert_id(),
  10. ‘firstname’ => $firstname,
  11. ‘lastname’ => $lastname,
  12. ‘phone’ => $phone,
  13. ’email’ => $email
  14. ));

Update an exists user(update_user.php):

  1. $id = intval($_REQUEST[‘id’]);
  2. $firstname = $_REQUEST[‘firstname’];
  3. $lastname = $_REQUEST[‘lastname’];
  4. $phone = $_REQUEST[‘phone’];
  5. $email = $_REQUEST[’email’];
  6. include ‘conn.php’;
  7. $sql = “update users set firstname=’$firstname’,lastname=’$lastname’,phone=’$phone’,email=’$email’ where id=$id”;
  8. @mysql_query($sql);
  9. echo json_encode(array(
  10. ‘id’ => $id,
  11. ‘firstname’ => $firstname,
  12. ‘lastname’ => $lastname,
  13. ‘phone’ => $phone,
  14. ’email’ => $email
  15. ));

Destroy an exists user(destroy_user.php):

  1. $id = intval($_REQUEST[‘id’]);
  2. include ‘conn.php’;
  3. $sql = “delete from users where id=$id”;
  4. @mysql_query($sql);
  5. echo json_encode(array(‘success’=>true));

Download the EasyUI example:



赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏