W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
用戶可以通過將數(shù)據(jù)網(wǎng)格視圖(datagrid view)切換到'detailview'來展開一行,展開的行下方將會顯示該行的詳細(xì)內(nèi)容。這個功能允許您為防止在明細(xì)行面板(panel)中的編輯表單(form)提供一些合適的布局(layout)。
在本節(jié)內(nèi)容中,我們使用數(shù)據(jù)網(wǎng)格(datagrid)組件來減小編輯表單(form)所占據(jù)空間。
<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);
}
});
為了給數(shù)據(jù)網(wǎng)格(DataGrid)應(yīng)用明細(xì)視圖,你需要在html頁面頭部引入'datagrid-detailview.js'文件。
我們使用'detailFormatter'函數(shù)來生成行明細(xì)內(nèi)容。在這種情況下,我們返回一個用于放置編輯表單(form)的空的<div>。當(dāng)用戶點擊行展開按鈕('+')時,將觸發(fā)'onExpandRow'事件,我們將通過ajax加載編輯表單(form)。調(diào)用'getRowDetail'方法來得到行明細(xì)容器,所以我們能查找到行明細(xì)面板(panel)。在行明細(xì)中創(chuàng)建面板(panel),加載從'show_form.php'返回的編輯表單(form)。
提示:有關(guān)ajax的使用,你可以參考本站的《AJAX教程》!
編輯表單(form)是從服務(wù)器加載的。
<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>
調(diào)用'saveItem'函數(shù)來保存一個用戶或者調(diào)用'cancelItem'函數(shù)來取消編輯。
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
});
}
});
}
確定要回傳的URL,然后查找表單(form)對象,并調(diào)用'submit'方法來提交表單(form)數(shù)據(jù)。當(dāng)保存數(shù)據(jù)成功時,折疊并更新行數(shù)據(jù)。
function cancelItem(index){
var row = $('#dg').datagrid('getRows')[index];
if (row.isNewRecord){
$('#dg').datagrid('deleteRow',index);
} else {
$('#dg').datagrid('collapseRow',index);
}
}
當(dāng)取消編輯動作時,如果該行是新行而且還沒有保存,直接刪除該行,否則折疊該行。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: