原創|使用教程|編輯:龔雪|2015-05-15 09:22:51.000|閱讀 1801 次
概述:本文主要為大家介紹如何使用jQuery EasyUI來創建展開行詳細編輯表單的CRUD應用。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Kendo UI for jQuery——創建現代Web應用程序的最完整UI庫!查看詳情>>>
當切換datagrid視圖到"detailview"時,用戶可以展開一行來顯示該行下面的任何詳細信息。此功能允許用戶為放置在行詳細信息面板中的編輯表單提供恰當的布局。在本教程中,我們使用DataGrid組件來減少編輯表單所占用的空間。
<table id="dg" title="My Users" style="width:550px;height:250px" url="get_users.php" toolbar="#toolbar" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="firstname" width="50">First Name</th> <th field="lastname" width="50">Last Name</th> <th field="phone" width="50">Phone</th> <th field="email" width="50">Email</th> </tr> </thead> </table> <div id="toolbar"> <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newItem()">New</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyItem()">Destroy</a> </div>
$('#dg').datagrid({ view: detailview, detailFormatter:function(index,row){ return '<div class="ddv"></div>'; }, 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); } });
若要使用DataGrid的詳細視圖,那么就在html 頁面頭部引入"datagrid-detailview.js"文件。
我們使用"detailFormatter"函數來生成行詳細信息內容。在這種情況下,我們返回一個用于放置編輯表單的空的 。當用戶點擊該行展開按鈕("+")時,"onExpandRow"事件將被觸發,我們可以通過AJAX加載編輯表單。使用getRowDetail方法可以獲得該行的詳細信息容器,因此我們能夠查找到該行的詳細信息面板。在行詳細信息中創建面板,并從"show_form.php"中加載返回的編輯表單。
從服務器中加載編輯表單。
show_form.php
<form method="post"> <table class="dv-table" style="width:100%;background:#fafafa;padding:5px;margin-top:5px;"> <tr> <td>First Name</td> <td><input name="firstname" class="easyui-validatebox" required="true"></input></td> <td>Last Name</td> <td><input name="lastname" class="easyui-validatebox" required="true"></input></td> </tr> <tr> <td>Phone</td> <td><input name="phone"></input></td> <td>Email</td> <td><input name="email" class="easyui-validatebox" validType="email"></input></td> </tr> </table> <div style="padding:5px 0;text-align:right;padding-right:30px"> <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="saveItem(<?php echo $_REQUEST['index'];?>)">Save</a> <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="cancelItem(<?php echo $_REQUEST['index'];?>)">Cancel</a> </div> </form>
調用"saveItem"函數來保存用戶,或調用"cancelItem"函數來取消編輯。
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 = ; data.isNewRecord = false; $('#dg').datagrid('collapseRow',index); $('#dg').datagrid('updateRow',{ index: index, row: data }); } }); }
確定首先要發布哪一個URL,然后查找表單對象,并調用"submit"方法來提交表單數據。當數據保存成功后,收起并更新行數據。
function cancelItem(index){ var row = $('#dg').datagrid('getRows')[index]; if (row.isNewRecord){ $('#dg').datagrid('deleteRow',index); } else { $('#dg').datagrid('collapseRow',index); } }
當取消編輯操作時,如果該行是新行而且還沒有保存,那么直接刪除該行,否則收起該行。
下載EasyUI示例:easyui-crud-demo.zip
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網