jQuery EasyUI Application - Creating a CRUD DataGrid (DataGrid)
In the previous chapter, we used the dialog component to create a CRUD app for creating and editing user information. In this tutorial, we will show you how to create a CRUD DataGrid. We will use the editable DataGrid plugin to accomplish these CRUD operations.
Step 1: Define the DataGrid in an HTML tag
First Name
Last Name
Phone
Email
Step 2: Use the editable DataGrid
$('#dg').edatagrid({
url: 'get_users.php',
saveUrl: 'save_user.php',
updateUrl: 'update_user.php',
destroyUrl: 'destroy_user.php'
});
We should provide the 'url', 'saveUrl', 'updateUrl', and 'destroyUrl' properties to edit the DataGrid:
- url: retrieve user data from the server.
- saveUrl: save a new user's data.
- updateUrl: update an existing user's data.
- destroyUrl: delete an existing user's data.
Step 3: Write the server-side handling code
Save a new user (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 an existing user (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
));
Delete an existing user (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));