在本教程中,咱們將爲你們展現如何建立一個CRUD DataGrid。在這期間將使用到可編輯的datagrid插件來同這些crud操做一塊兒工做。javascript
<table id="dg" title="My Users" style="width:550px;height:250px"php
toolbar="#toolbar" idField="id"html
rownumbers="true" fitColumns="true" singleSelect="true">java
<thead>mysql
<tr>sql
<th field="firstname" width="50" editor="{type:'validatebox',options:{required:true}}">First Name</th>json
<th field="lastname" width="50" editor="{type:'validatebox',options:{required:true}}">Last Name</th>服務器
<th field="phone" width="50" editor="text">Phone</th>ui
<th field="email" width="50" editor="{type:'validatebox',options:{validType:'email'}}">Email</th>url
</tr>
</thead>
</table>
<div id="toolbar">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg').edatagrid('addRow')">New</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg').edatagrid('destroyRow')">Destroy</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:$('#dg').edatagrid('saveRow')">Save</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="javascript:$('#dg').edatagrid('cancelRow')">Cancel</a>
</div>
$('#dg').edatagrid({
url: 'get_users.php',
saveUrl: 'save_user.php',
updateUrl: 'update_user.php',
destroyUrl: 'destroy_user.php'
});
咱們應該爲可編輯的datagrid提供"url"、"saveUrl"、"updateUrl"和"destroyUrl"屬性:
url:從服務器獲取用戶數據。
saveUrl:保存一個新用戶的數據。
updateUrl:更新一個已存在的用戶的數據。
destroyUrl:刪除一個已存在的用戶的數據。
保存一個新用戶(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):
$id = intval($_REQUEST['id']);
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
include 'conn.php';
$sql="update users set firstname='$firstname',lastname='$lastname',phone='$phone',email='$email' where id=$id";
@mysql_query($sql);
echo json_encode(array(
'id' => $id,
'firstname' => $firstname,
'lastname' => $lastname,
'phone' => $phone,
'email' => $email
));
刪除一個已存在的用戶(destroy_user.php):
$id = intval($_REQUEST['id']);
include 'conn.php';
$sql = "delete from users where id=$id";
@mysql_query($sql);
echo json_encode(array('success'=>true));
下載EasyUI示例:easyui-crud-demo.zip
有興趣的朋友能夠點擊查看更多有關jQuery EasyUI的教程!