jQuery EasyUI使用教程之構建CRUD DataGrid

在本教程中,咱們將爲你們展現如何建立一個CRUD DataGrid。在這期間將使用到可編輯的datagrid插件來同這些crud操做一塊兒工做。javascript

構建CRUD DataGrid

Step 1:在HTML標記中定義DataGrid

<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>

Step 2:製做一個可編輯的DataGrid

$('#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:刪除一個已存在的用戶的數據。

Step 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):

$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試用版下載地址<<

有興趣的朋友能夠點擊查看更多有關jQuery EasyUI的教程

相關文章
相關標籤/搜索