標籤:
當切換資料網路格局(datagrid view)到detailview,使用者可以展開一行來顯示一些行的明細在行下面,這個功能允許您為防止在明細行面板中的編輯表單提供一些合適的布局。
步驟1、在HTML標籤中定義資料格DataGrid
1 <table id="dg" title="My Users" style="width:550px;height:250px" 2 url="get_users.php" 3 toolbar="#toolbar" 4 fitColumns="true" singleSelect="true"> 5 <thead> 6 <tr> 7 <th field="firstname" width="50">First Name</th> 8 <th field="lastname" width="50">Last Name</th> 9 <th field="phone" width="50">Phone</th>10 <th field="email" width="50">Email</th>11 </tr>12 </thead>13 </table>14 <div id="toolbar">15 <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newItem()">New</a>16 <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyItem()">Destroy</a>17 </div>
步驟2、為資料格應用明細視圖
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 });
為了為資料格(DataGrid)應用明細視圖,在 html 頁面頭部引入 ‘datagrid-detailview.js‘ 檔案。
我們使用 ‘detailFormatter‘ 函數來產生行明細內容。 在這種情況下,我們返回一個用於放置編輯表單(form)的空的 <div>。 當使用者點擊行展開按鈕(‘+‘)時,‘onExpandRow‘ 事件將被觸發,我們將通過 ajax 載入編輯表單(form)。 調用 ‘getRowDetail‘ 方法來得到行明細容器,所以我們能尋找到行明細面板(panel)。 在行明細中建立面板(panel),載入從 ‘show_form.php‘ 返回的編輯表單(form)。
步驟3、建立編輯表單Form
1 <form method="post"> 2 <table class="dv-table" style="width:100%;background:#fafafa;padding:5px;margin-top:5px;"> 3 <tr> 4 <td>First Name</td> 5 <td><input name="firstname" class="easyui-validatebox" required="true"></input></td> 6 <td>Last Name</td> 7 <td><input name="lastname" class="easyui-validatebox" required="true"></input></td> 8 </tr> 9 <tr>10 <td>Phone</td>11 <td><input name="phone"></input></td>12 <td>Email</td>13 <td><input name="email" class="easyui-validatebox" validType="email"></input></td>14 </tr>15 </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>
步驟4.儲存或取消編輯
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: data16 });17 }18 });19 }
決定要回傳哪個URL,然後尋找表單對象,並調用submit方法提交表單資料,當儲存資料成功後,摺疊並更新行資料
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 }
【jQuery EasyUI系列】 建立展開行明細編輯表單的CRUD應用