使用easyUI创建一个CRUD DataGrid

@author YHC

在以前的示例中我们创建crud应用使用对话框(dialog)组件创建和编辑用户信息,这个教程将告诉你如何创建一个crud datagrid,我们将使用可编辑的datagrid插件做这些crud操作动作.


查看DEMO

步骤1:在HTML标记中定义一个DataGrid
<table id="dg" title="My Users" style="width:550px;height:250px"  
        toolbar="#toolbar"  
        rownumbers="true" fitColumns="true" singleSelect="true">  
    <thead>  
        <tr>  
            <th field="firstname" width="50" editor="{type:'validatebox',options:{required:true}}">First Name</th>  
            <th field="lastname" width="50" editor="{type:'validatebox',options:{required:true}}">Last Name</th>  
            <th field="phone" width="50" editor="text">Phone</th>  
            <th field="email" width="50" editor="{type:'validatebox',options:{validType:'email'}}">Email</th>  
        </tr>  
    </thead>  
</table>  
<div id="toolbar">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" οnclick="javascript:$('#dg').edatagrid('addRow')">New</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" οnclick="javascript:$('#dg').edatagrid('destroyRow')">Destroy</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" οnclick="javascript:$('#dg').edatagrid('saveRow')">Save</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-undo" plain="true" οnclick="javascript:$('#dg').edatagrid('cancelRow')">Cancel</a>  
</div>  
步骤2: 使DataGrid可编辑

$('#dg').edatagrid({  
    url: 'get_users.php',  
    saveUrl: 'save_user.php',  
    updateUrl: 'update_user.php',  
    destroyUrl: 'destroy_user.php'  
}); 
我们应该提供 'url','saveUrl','updateUrl' 和 'destroyUrl' 属性来编辑datagrid

  • url:从远程服务器端检索数据.
  • saveUrl: 保存一个新的用户数据.
  • updateUrl: 更新一个存在的用户的数据.
  • destroyUrl:删除一个已存在的用户.
步骤3:写服务器端处理代码
保存一个新的用户(save_user.php):
$firstname = $_REQUEST['firstname'];  
$lastname = $_REQUEST['lastname'];  
$phone = $_REQUEST['phone'];  
$email = $_REQUEST['email'];  
  
include 'conn.php';  
  
$sql = "insert into users(firstname,lastname,phone,email) values('$firstname','$lastname','$phone','$email')";  
@mysql_query($sql);  
echo json_encode(array(  
    'id' => mysql_insert_id(),  
    'firstname' => $firstname,  
    'lastname' => $lastname,  
    'phone' => $phone,  
    'email' => $email  
));  
更新一个已存在用户(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.   
  7. include 'conn.php';  
  8.   
  9. $sql = "update users set firstname='$firstname',lastname='$lastname',phone='$phone',email='$email' where id=$id";  
  10. @mysql_query($sql);  
  11. echo json_encode(array(  
  12.     'id' => $id,  
  13.     'firstname' => $firstname,  
  14.     'lastname' => $lastname,  
  15.     'phone' => $phone,  
  16.     'email' => $email  
  17. ));  
删除一个已存在用户(destroy_user.php):
  1. $id = intval($_REQUEST['id']);  
  2.   
  3. include 'conn.php';  
  4.   
  5. $sql = "delete from users where id=$id";  
  6. @mysql_query($sql);  
  7. echo json_encode(array('success'=>true)); 
下载EasyUI 示例代码:
    easyui_crud_demo.zip




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值