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

jQuery EasyUI Application - Creating a CRUD App with Expanded Row Detail Edit Forms

jQuery EasyUI Application - Creating a CRUD App with Expanded Row Detail Edit Forms

When switching the datagrid view to 'detailview', users can expand a row to show some row details below it. This feature allows you to provide a suitable layout for the edit form in the detail row panel. In this tutorial, we use the datagrid component to reduce the space occupied by the edit form.

Step 1: Define the DataGrid in an HTML tag





    

        

            

            

            

            

        

    
First NameLast NamePhoneEmail
    New     Destroy

Step 2: Apply the detail view to the DataGrid



$('#dg').datagrid({

    view: detailview,

    detailFormatter:function(index,row){

        return '
';     },     onExpandRow: function(index,row){         var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');         ddv.panel({             border:false,             cache:true,             href:'show_form.php?index='+index,             onLoad:function(){                 $('#dg').datagrid('fixDetailRowHeight',index);                 $('#dg').datagrid('selectRow',index);                 $('#dg').datagrid('getRowDetail',index).find('form').form('load',row);             }         });         $('#dg').datagrid('fixDetailRowHeight',index);     } });

To apply the detail view to the DataGrid, include the 'datagrid-detailview.js' file in the head of the html page.

We use the 'detailFormatter' function to generate the row detail content.

In this case, we return an empty

。

When the user clicks the row expand button ('+'), the 'onExpandRow' event is triggered, and we load the edit form via ajax.

Call the 'getRowDetail' method to get the row detail container, so we can find the detail row panel.

Create a panel in the row detail and load the edit form returned from 'show_form.php'.

Step 3: Create the edit form (Form)

The edit form is loaded from the server.

show_form.php



                                                                                                                                                     
First NameLast Name
PhoneEmail
    
        Save         Cancel     

Step 4: Save or cancel editing

Call the 'saveItem' function to save a user, or call the 'cancelItem' function to cancel editing.



function saveItem(index){

    var row = $('#dg').datagrid('getRows')[index];

    var url = row.isNewRecord ? 'save_user.php' : 'update_user.php?id='+row.id;

    $('#dg').datagrid('getRowDetail',index).find('form').form('submit',{

        url: url,

        onSubmit: function(){

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

        },

        success: function(data){

            data = eval('('+data+')');

            data.isNewRecord = false;

            $('#dg').datagrid('collapseRow',index);

            $('#dg').datagrid('updateRow',{

                index: index,

                row: data

            });

        }

    });

}

Decide which URL to post back to, then find the form object and call the 'submit' method to submit the form data. When the data is saved successfully, collapse and update the row data.



function cancelItem(index){

    var row = $('#dg').datagrid('getRows')[index];

    if (row.isNewRecord){

        $('#dg').datagrid('deleteRow',index);

    } else {

        $('#dg').datagrid('collapseRow',index);

    }

}

When canceling the edit, if the row is new and has not been saved, delete the row directly; otherwise, collapse the row.

Download the jQuery EasyUI example

jeasyui-app-crud3.zip