Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery EasyUI Application - Creating a CRUD App

jQuery EasyUI Application - Creating a CRUD App

Collecting and properly managing data is a common necessity for web applications. CRUD allows us to generate page lists and edit database records. This tutorial will show you how to implement a CRUD DataGrid using the jQuery EasyUI framework.

We will use the following plugins:

Step 1: Prepare the database

We will use a MySQL database to store user information. Create the database and the 'users' table.

Step 2: Create a DataGrid to display user information

Create a DataGrid without any JavaScript code.





    

        

            

            

            

            

        

    
First NameLast NamePhoneEmail
    New User     Edit User     Remove User

We do not need to write any JavaScript code to display the list to users, as shown below:

The DataGrid uses the 'url' property, assigned the value 'get_users.php', to retrieve data from the server.

The code for the get_users.php file



$rs = mysql_query('select * from users');

$result = array();

while($row = mysql_fetch_object($rs)){

    array_push($result, $row);

}



echo json_encode($result);

Step 3: Create the form dialog

We use the same dialog to create or edit users.



    
User Information
    
        
                                  
        
                                  
        
                                  
        
                                  
    
    Save     Cancel

This dialog is already created, also without any JavaScript code:

Step 4: Implement creating and editing users

When creating a user, open a dialog and clear the form data.



function newUser(){

    $('#dlg').dialog('open').dialog('setTitle','New User');

    $('#fm').form('clear');

    url = 'save_user.php';

}

When editing a user, open a dialog and load the form data from the row selected in the datagrid.



var row = $('#dg').datagrid('getSelected');

if (row){

    $('#dlg').dialog('open').dialog('setTitle','Edit User');

    $('#fm').form('load',row);

    url = 'update_user.php?id='+row.id;

}

'url' stores the URL to which the form posts back when saving user data.

Step 5: Save user data

We use the following code to save user data:



function saveUser(){

    $('#fm').form('submit',{

        url: url,

        onSubmit: function(){

            return $(this).form('validate');

        },

        success: function(result){

            var result = eval('('+result+')');

            if (result.errorMsg){

                $.messager.show({

                    title: 'Error',

                    msg: result.errorMsg

                });

            } else {

                $('#dlg').dialog('close');        // close the dialog

                $('#dg').datagrid('reload');    // reload the user data

            }

        }

    });

}

Before submitting the form, the 'onSubmit' function is called to validate the form field values. When the form values are submitted successfully, close the dialog and reload the datagrid data.

Step 6: Delete a user

We use the following code to remove a user:



function destroyUser(){

    var row = $('#dg').datagrid('getSelected');

    if (row){

        $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){

            if (r){

                $.post('destroy_user.php',{id:row.id},function(result){

                    if (result.success){

                        $('#dg').datagrid('reload');    // reload the user data

                    } else {

                        $.messager.show({    // show error message

                            title: 'Error',

                            msg: result.errorMsg

                        });

                    }

                },'json');

            }

        });

    }

}

Before removing a row, we display a confirmation dialog to let the user decide whether to really remove the row data. After the data is removed successfully, call the 'reload' method to refresh the datagrid data.

Step 7: Run the code

Start MySQL and run the code in a browser.

Download the jQuery EasyUI example

jeasyui-app-crud1.zip