jquery Easyui Datagrid實(shí)現(xiàn)批量操作(編輯,刪除,添加)
有時候我們的后臺系統(tǒng)表單比較復(fù)雜,做過進(jìn)銷存或者一些銷售訂單的都應(yīng)該有過感覺。
雖然Easyui Datagrid提供了行內(nèi)編輯,但是不夠靈活,但是我們稍微修改一下來達(dá)到批量編輯,批量刪除,批量添加的效果。
現(xiàn)在我們來看看原的編輯:來自Easyui 1.5.1的Demo <demo/datagrid/rowediting.html>

接下來,我們主要是要高度自由的編輯實(shí)現(xiàn):
1.可以同時追加多行
2.追加的行可以是任何位置
3.可以隨時進(jìn)行編輯任意位置的行
4.保存再統(tǒng)一驗(yàn)證
實(shí)現(xiàn)
在原有的rowediting.html進(jìn)行修改!
第一:修改行的點(diǎn)擊事件(點(diǎn)擊行的時候進(jìn)入編輯狀態(tài))
function onClickCell(index, field){
if (editIndex != index) {
if (endEditing()) {
$('#dg').datagrid('selectRow', index)
.datagrid('beginEdit', index);
var ed = $('#dg').datagrid('getEditor', { index: index, field: field });
if (ed) {
($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
}
editIndex = index;
} else {
setTimeout(function () {
$('#dg').datagrid('selectRow', editIndex);
}, 0);
}
}
}

第二:刪除事件(點(diǎn)擊頂部菜單Remove刪除選中的行,點(diǎn)擊列表的-號,刪除減號行)
function removeit(){
if (editIndex == undefined){return}
$('#dg').datagrid('selectRow', editIndex);
$('#dg').datagrid('cancelEdit', editIndex)
.datagrid('deleteRow', editIndex);
editIndex = undefined;
}

第三:添加事件,點(diǎn)擊菜單的Append和+號
function append(){
var index = $('#dg').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
if (index == -1)
index = 0;
$("#dg").datagrid("insertRow", {
index: index+1,
row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
});
}

第四:保存(獲得操作的記錄,包括,增加,修改,刪除中的記錄)
function accept(){
if (endEditing()){
var $dg = $('#dg');
var rows = $dg.datagrid('getChanges');
if (rows.length) {
var inserted = $dg.datagrid('getChanges', "inserted");
var deleted = $dg.datagrid('getChanges', "deleted");
var updated = $dg.datagrid('getChanges', "updated");
var effectRow = new Object();
if (inserted.length) {
effectRow["inserted"] = JSON.stringify(inserted);
}
if (deleted.length) {
effectRow["deleted"] = JSON.stringify(deleted);
}
if (updated.length) {
effectRow["updated"] = JSON.stringify(updated);
}
//alert(inserted);
//alert(deleted);
//alert(updated);
}
}
//$.post("/Home/Commit", effectRow, function (rsp) {
// if (rsp) {
// $dg.datagrid('acceptChanges');
// bindData();
// }
//}, "JSON").error(function () {
// $.messager.alert("提示", "提交錯誤了!");
//});
}
最后我們可以獲得,上面操作的,所有:添加的行,刪除的行,更新的行!把數(shù)據(jù)傳入到數(shù)據(jù)后臺進(jìn)行處理!
最后你還需要對數(shù)據(jù)進(jìn)行循環(huán)校驗(yàn),可以獲得數(shù)據(jù),在控制臺輸出:
console.log(inserted); console.log(deleted); console.log(updated);

總結(jié):
最后完整代碼:(替換Easyui的rowediting.html可運(yùn)行效果)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Row Editing in DataGrid - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" rel="external nofollow" >
<link rel="stylesheet" type="text/css" href="../../themes/icon.css" rel="external nofollow" >
<link rel="stylesheet" type="text/css" href="../demo.css" rel="external nofollow" >
<script type="text/javascript" src="../../jquery.min.js"></script>
<script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>
<h2>Row Editing in DataGrid</h2>
<p>Click the row to start editing.</p>
<div style="margin:20px 0;"></div>
<table id="dg" class="easyui-datagrid" title="Row Editing in DataGrid" style="width:800px;height:auto"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
toolbar: '#tb',
url: 'datagrid_data1.json',
method: 'get',
onClickCell: onClickCell,
onEndEdit: onEndEdit
">
<thead>
<tr>
<th data-options="field:'oper',width:80">操作</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<th data-options="field:'productid',width:100,
formatter:function(value,row){
return row.productname;
},
editor:{
type:'combobox',
options:{
valueField:'productid',
textField:'productname',
method:'get',
url:'products.json',
}
}">Product</th>
<th data-options="field:'listprice',width:80,align:'right',editor:{type:'numberbox',options:{precision:1}}">List Price</th>
<th data-options="field:'unitcost',width:80,align:'right',editor:'numberbox'">Unit Cost</th>
<th data-options="field:'attr1',width:250,editor:'textbox'">Attribute</th>
<th data-options="field:'status',width:60,align:'center',editor:{type:'checkbox',options:{on:'P',off:''}}">Status</th>
</tr>
</thead>
</table>
<div id="tb" style="height:auto">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">Append</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">Remove</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">Accept</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">Reject</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">GetChanges</a>
</div>
<script type="text/javascript">
//編輯的行
var editIndex = undefined;
function endEditing() {
if (editIndex == undefined){return true}
$('#dg').datagrid('endEdit', editIndex);
editIndex = undefined;
return true;
}
function onClickCell(index, field){
if (editIndex != index) {
if (endEditing()) {
$('#dg').datagrid('selectRow', index)
.datagrid('beginEdit', index);
var ed = $('#dg').datagrid('getEditor', { index: index, field: field });
if (ed) {
($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
}
editIndex = index;
} else {
setTimeout(function () {
$('#dg').datagrid('selectRow', editIndex);
}, 0);
}
}
}
function onEndEdit(index, row){
var ed = $(this).datagrid('getEditor', {
index: index,
field: 'productid'
});
row.productname = $(ed.target).combobox('getText');
}
function append(){
var index = $('#dg').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
if (index == -1)
index = 0;
$("#dg").datagrid("insertRow", {
index: index+1,
row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
});
}
function removeit(){
if (editIndex == undefined){return}
$('#dg').datagrid('selectRow', editIndex);
$('#dg').datagrid('cancelEdit', editIndex)
.datagrid('deleteRow', editIndex);
editIndex = undefined;
}
function accept(){
if (endEditing()){
var $dg = $('#dg');
var rows = $dg.datagrid('getChanges');
if (rows.length) {
var inserted = $dg.datagrid('getChanges', "inserted");
var deleted = $dg.datagrid('getChanges', "deleted");
var updated = $dg.datagrid('getChanges', "updated");
var effectRow = new Object();
if (inserted.length) {
effectRow["inserted"] = JSON.stringify(inserted);
}
if (deleted.length) {
effectRow["deleted"] = JSON.stringify(deleted);
}
if (updated.length) {
effectRow["updated"] = JSON.stringify(updated);
}
//alert(inserted);
//alert(deleted);
//alert(updated);
}
}
//$.post("/Home/Commit", effectRow, function (rsp) {
// if (rsp) {
// $dg.datagrid('acceptChanges');
// bindData();
// }
//}, "JSON").error(function () {
// $.messager.alert("提示", "提交錯誤了!");
//});
}
function reject(){
$('#dg').datagrid('rejectChanges');
editIndex = undefined;
}
function getChanges(){
var rows = $('#dg').datagrid('getChanges');
alert(rows.length+' rows are changed!');
}
function contains(arr, obj) {
var i = arr.length;
while (i--) {
if (arr[i] === obj) {
return true;
}
}
return false;
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery EasyUI API 中文文檔 - DataGrid數(shù)據(jù)表格
- Jquery下EasyUI組件中的DataGrid結(jié)果集清空方法
- jQuery easyui datagrid動態(tài)查詢數(shù)據(jù)實(shí)例講解
- 擴(kuò)展easyui.datagrid,添加數(shù)據(jù)loading遮罩效果代碼
- jQuery EasyUI datagrid實(shí)現(xiàn)本地分頁的方法
- jQuery EasyUI之DataGrid使用實(shí)例詳解
- jQuery Easyui DataGrid點(diǎn)擊某個單元格即進(jìn)入編輯狀態(tài)焦點(diǎn)移開后保存數(shù)據(jù)
- 實(shí)現(xiàn)easyui的datagrid導(dǎo)出為excel的示例代碼
- 詳解EasyUi控件中的Datagrid
- EasyUI使用DataGrid實(shí)現(xiàn)動態(tài)列數(shù)據(jù)綁定
相關(guān)文章
擴(kuò)展Jquery插件處理mouseover時內(nèi)部有子元素時發(fā)生樣式閃爍
在我們對一個dom添加mouseover和mouseout時,也就是jquery的hover事件,如果該dom有子元素,鼠標(biāo)移到子元素時會觸發(fā)mouseout事件,但往往實(shí)際情況我們希望在子元素上不觸發(fā)out事件2011-12-12
jquery ui resize 中border-box的bug修正
本文給大家分享的是jQuery ui resize中的一個樣式的小bug的解決方法,官方并沒有修復(fù),這里推薦給大家,有需要的小伙伴可以參考下。2015-04-04
使用jquery實(shí)現(xiàn)別踩白塊小游戲的方法實(shí)例
別踩白塊是一款簡單易上手的數(shù)字小游戲,下面這篇文章主要給大家介紹了關(guān)于使用jquery實(shí)現(xiàn)別踩白塊小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
jquery插件制作 自增長輸入框?qū)崿F(xiàn)代碼
本章我們將創(chuàng)建一個自增長的輸入框插件,jquery.aotogrow.js2012-08-08
jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級聯(lián)動
本文給大家分享jQuery EasyUI編輯DataGrid用combobox實(shí)現(xiàn)多級聯(lián)動效果的實(shí)例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-08-08

