1、实例化两个数组
var idsArray = new Array(); //记录勾选的id
var UnidsArray = new Array(); //记录取消勾选的id
j's代码如下:
function loadingDatagridCheckNew(url, columns, thisid, bol) {
var _thid = "ID";
if (thisid != null) {
_thid = thisid;
}
var bv = true;
if (bol != null) {
bv = bol;
}
$('#magazineGrid').datagrid({
height: $(window).height() - $('fieldset').height() - 50,
width: 'auto',
url: url,//地址
method: 'POST',
queryParams: queryParams,
idField: _thid,
striped: true,
fitColumns: false,// true,//宽度自适应
singleSelect: bv,
rownumbers: true,
pagination: true,
nowrap: true,// false,//允许换行
emptyMsg: '无记录',
loadMsg: '正在加载中,请稍等... ',
pageSize: 20,
pageList: [10, 20, 50, 100, 150, 200],
showFooter: true,
columns: columns,
selectOnCheck: true,
checkOnSelect: true,
toolbar: '#datagrid-toolbar',
onBeforeLoad: function (param) {
addSelectedGoodsIdToArray();
},
onLoadSuccess: function (data) {
if ($(".easyui-tooltip")) {
$(".easyui-tooltip").tooltip({
position: 'left'
});
}
if (data) {
$.each(data.rows, function (index, item) {
if (item.Checked) { //数据库记录勾选的字段,加载的时候自动绑定
if (!UnidsArray.includes(item.SST_ID)) {
$('#magazineGrid').datagrid('checkRow', index);
}
}
});
}
var rowsData = data.rows;
if (idsArray.length != 0) {
// $.each(idsArray, function (i, e) {
for (var index = 0; index < rowsData.length; index++) {
if (idsArray.includes(rowsData[index].SST_ID)) {
$('#magazineGrid').datagrid('selectRow', index);
} else {
if (UnidsArray.includes(rowsData[index].SST_ID)) {
$('#magazineGrid').datagrid('unselectRow', index);
}
}
}
// });
}
},
onUncheck: function (rowIndex, rowData) {
if (idsArray.length == 0) {
UnidsArray.push(rowData["SST_ID"]);
} else {
for (var index = 0; index < idsArray.length; index++) {
if (idsArray[index] == rowData["SST_ID"]) {
removeArrayValue(idsArray, rowData["SST_ID"]);
break;
}
}
}
},
onCheck: function (rowIndex, rowData) {
if (idsArray.length == 0) {
idsArray.push(rowData["SST_ID"]);
} else {
for (var index = 0; index < idsArray.length; index++) {
if (idsArray[index] == rowData["SST_ID"]) {
break;
}
if (index == idsArray.length - 1) {
idsArray.push(rowData["SST_ID"]);
break;
}
}
}
},
onSelectAll: function (rows) {
if (idsArray.length == 0) {
$.each(rows, function (index, item) {
idsArray.push(item["SST_ID"]);
});
}
else {
$.each(rows, function (index, item) {
for (var index = 0; index < idsArray.length; index++) {
if (idsArray[index] == item["SST_ID"]) {
break;
}
if (index == idsArray.length - 1) {
idsArray.push(item["SST_ID"]);
break;
}
}
});
}
},
//onUnselectAll: function (rows) {
// alert('onUnselectAll');
//},
//onCheckAll: function (rows) {
// alert("onCheckAll");
//},
onUncheckAll: function (rows) {
if (idsArray.length == 0) {
$.each(rows, function (index, item) {
UnidsArray.push(item["SST_ID"]);
});
}
else {
$.each(rows, function (index, item) {
for (var i = 0; i < idsArray.length; i++) {
if (idsArray[i] == item["SST_ID"]) {
removeArrayValue(idsArray, item["SST_ID"]);
break;
}
}
});
}
}
});
}
function addSelectedGoodsIdToArray() {
var rows = $('#magazineGrid').datagrid('getSelections');
if (rows.length > 0) {
$.each(rows, function (i, row) {
if (idsArray.length == 0) {
if (UnidsArray.indexOf(row["SST_ID"]) == -1) {
idsArray.push(row["SST_ID"]);
}
} else {
for (var index = 0; index < idsArray.length; index++) {
if (idsArray[index] == row["SST_ID"]) {
break;
}
if (index == idsArray.length - 1) {
if (UnidsArray.indexOf(row["SST_ID"]) == -1) {
idsArray.push(row["SST_ID"]);
}
break;
}
}
}
});
}
}
function removeSelectedGoodsIdToArray(rows) {
//var rows = $dg.datagrid('getSelections');
if (rows.length > 0) {
$.each(rows, function (i, row) {
if (idsArray.length > 0) {
for (var index = 0; index < idsArray.length; index++) {
removeArrayValue(idsArray, row["SST_ID"]);
}
}
});
}
}
function removeArrayValue(arr, val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == val) {
arr.splice(i, 1);
UnidsArray.push(val);
break;
}
}
}
保存的时候只取idsArray这是勾选的数组,UnidsArray这是不勾选的数组传到后台,后台业务先把取消的从数据库删除了,然后再存勾选的。