[转载]Build CRUD Application with edit form in expanded row details - jQuery EasyUI

[转载]Build CRUD Application with edit form in expanded row details – jQuery EasyUI.

Build CRUD Application with edit form in expanded row details

Tutorial » Build CRUD Application with edit form in expanded row details

When switch the datagrid view to ‘detailview’, users can expand a row to show any details below that row. This feature allows you to provide any possible layout for its edit form that placed in detail row panel. In this tutorial, we are using datagrid component to reduce the space occupied by the edit form.

View Demo

Step 1: Create DataGrid in HTML tag

  1. <table id=“dg” title=“My Users” style=“width:550px;height:250px” url=“get_users.php” toolbar=“#toolbar” fitcolumns=“true” singleselect=“true”>
  2. <thead>
  3. <tr>
  4. <th field=“firstname” width=“50”>First Name</th>
  5. <th field=“lastname” width=“50”>Last Name</th>
  6. <th field=“phone” width=“50”>Phone</th>
  7. <th field=“email” width=“50”>Email</th>
  8. </tr>
  9. </thead>
  10. </table>
  11. <div id=“toolbar”>
  12. <a href=“#” class=“easyui-linkbutton” iconcls=“icon-add” plain=“true” onclick=“newItem()”>New</a>
  13. <a href=“#” class=“easyui-linkbutton” iconcls=“icon-remove” plain=“true” onclick=“destroyItem()”>Destroy</a>
  14. </div>

Step 2: Apply Detail View for DataGrid

  1. $(‘#dg’).datagrid({
  2. view: detailview,
  3. detailFormatter:function(index,row){
  4. return ‘<div class=”ddv”></div>’;
  5. },
  6. onExpandRow: function(index,row){
  7. var ddv = $(this).datagrid(‘getRowDetail’,index).find(‘div.ddv’);
  8. ddv.panel({
  9. border:false,
  10. cache:true,
  11. href:‘show_form.php?index=’+index,
  12. onLoad:function(){
  13. $(‘#dg’).datagrid(‘fixDetailRowHeight’,index);
  14. $(‘#dg’).datagrid(‘selectRow’,index);
  15. $(‘#dg’).datagrid(‘getRowDetail’,index).find(‘form’).form(‘load’,row);
  16. }
  17. });
  18. $(‘#dg’).datagrid(‘fixDetailRowHeight’,index);
  19. }
  20. });

To use the detail view for datagrid, include the ‘datagrid-detailview.js’ file to your page header.

We use the ‘detailFormatter’ function to generate the row detail content. In this case, we return the empty <div> that the edit form will be placed in. When users click the row expand button(‘+’), the ‘onExpandRow’ event will be triggered and we can load the edit form via ajax. Call ‘getRowDetail’ method to get the row detail container, so we can find the row detail panel. Create the panel in row details and load the edit form that returned from ‘show_form.php’.

Step 3: Create Edit Form

The edit form is loaded from server.

show_form.php
  1. <form method=“post”>
  2. <table class=“dv-table” style=“width:100%;background:#fafafa;padding:5px;margin-top:5px;”>
  3. <tbody><tr>
  4. <td>First Name</td>
  5. <td><input name=“firstname” class=“easyui-validatebox” required=“true”></td>
  6. <td>Last Name</td>
  7. <td><input name=“lastname” class=“easyui-validatebox” required=“true”></td>
  8. </tr>
  9. <tr>
  10. <td>Phone</td>
  11. <td><input name=“phone”></td>
  12. <td>Email</td>
  13. <td><input name=“email” class=“easyui-validatebox” validtype=“email”></td>
  14. </tr>
  15. </tbody></table>
  16. <div style=“padding:5px 0;text-align:right;padding-right:30px”>
  17. <a href=“#” class=“easyui-linkbutton” iconcls=“icon-save” plain=“true” onclick=“saveItem(<?php echo $_REQUEST[‘index’];?>)”>Save</a>
  18. <a href=“#” class=“easyui-linkbutton” iconcls=“icon-cancel” plain=“true” onclick=“cancelItem(<?php echo $_REQUEST[‘index’];?>)”>Cancel</a>
  19. </div>
  20. </form>

Step 4: Save or Cancel editing

Call the ‘saveItem’ function to save a user or ‘cancelItem’ function to cancel the editing.

  1. function saveItem(index){
  2. var row = $(‘#dg’).datagrid(‘getRows’)[index];
  3. var url = row.isNewRecord ? ‘save_user.php’ : ‘update_user.php?id=’+row.id;
  4. $(‘#dg’).datagrid(‘getRowDetail’,index).find(‘form’).form(‘submit’,{
  5. url: url,
  6. onSubmit: function(){
  7. return $(this).form(‘validate’);
  8. },
  9. success: function(data){
  10. data = eval(‘(‘+data+‘)’);
  11. data.isNewRecord = false;
  12. $(‘#dg’).datagrid(‘collapseRow’,index);
  13. $(‘#dg’).datagrid(‘updateRow’,{
  14. index: index,
  15. row: data
  16. });
  17. }
  18. });
  19. }

Determine what is the URL to post to first and then find form object and call ‘submit’ method to post the form data. Collapse and update the row data while saving data successfully.

  1. function cancelItem(index){
  2. var row = $(‘#dg’).datagrid(‘getRows’)[index];
  3. if (row.isNewRecord){
  4. $(‘#dg’).datagrid(‘deleteRow’,index);
  5. else {
  6. $(‘#dg’).datagrid(‘collapseRow’,index);
  7. }
  8. }

When cancel the editing action, if the row is new and not save yet, delete the row directly, otherwise collapse the row.

Download the EasyUI example:

easyui-crud-demo.zip
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏