jQuery EasyUI アプリ -CRUD データグリッド(DataGrid)の作成

前の章では、ダイアログ(dialog)コンポーネントを使用して、ユーザー情報を作成・編集するCRUDアプリを作成しました。このチュートリアルでは、CRUDデータグリッド(DataGrid)を作成する方法を説明します。使用するのは、編集可能なデータグリッド(DataGrid)プラグインで、これらのCRUD操作を実行します。

ステップ1:HTMLタグでデータグリッド(DataGrid)を定義する

<table id="dg" title="My Users" style="width:550px;height:250px"
        toolbar="#toolbar" idField="id"
        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="../index.html" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg').edatagrid('addRow')">New</a>
    <a href="../index.html" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg').edatagrid('destroyRow')">Destroy</a>
    <a href="../index.html" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:$('#dg').edatagrid('saveRow')">Save</a>
    <a href="../index.html" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="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'
});

データグリッド(DataGrid)を編集するには、'url'、'saveUrl'、'updateUrl'、'destroyUrl' プロパティを提供する必要があります:

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

$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));

jQuery EasyUI サンプルをダウンロード

jeasyui-app-crud2.zip

その他の拡張