// 初始化DataGrid对象
$('#dataGrid').dataGrid({
searchForm: $("#searchForm"),
showCheckbox: true,
columnModel: [
{header:'${text("主键")}', name:'id', index:'a.id', width:150, align:"center",hidden:true},
//code作为回调页面保存使用,value仅作为回调页面的展示使用
{header:'${text("拍摄类型code")}', name:'categoryShoot', index:'a.category_shoot', width:150, align:"center",hidden:true},
{header:'${text("拍摄类型")}', name:'categoryShootName', index:'a.category_shoot', width:150, align:"center"},
{header:'${text("分类code")}', name:'categorySuite', index:'a.category_suite', width:150, align:"center",hidden:true},
{header:'${text("分类")}', name:'categorySuiteName', index:'a.category_suite', width:150, align:"center"},
{header:'${text("拍摄产品code")}', name:'categoryProduct', index:'a.category_product', width:150, align:"center",hidden:true},
{header:'${text("拍摄产品")}', name:'categoryProductName', index:'a.category_product', width:150, align:"center"},
{header:'${text("拍摄效果code")}', name:'categoryEffect', index:'a.category_effect', width:150, align:"center",hidden:true},
{header:'${text("拍摄效果")}', name:'categoryEffectName', index:'a.category_effect', width:150, align:"center"},
{header:'${text("单价(件/套)")}', name:'price', index:'a.price',width:100, align:"right", formatter:'integer', formatoptions:{
thousandsSeparator:',', defaulValue:''
}}
],
//每一行的选中事件
onSelectRow: function (id, isSelect, event) {
addOneGridSelect(id, isSelect);
},
//选择所有行
onSelectAll:function(rowIds, isSelect){
addAllGridSelect(rowIds, isSelect);
},
//表格加载完后事件
gridComplete:function(){
dataGridSelection();
},
// 加载成功后执行事件
ajaxSuccess: function(data){
}
});
再添加以下三个方法
//表格加载完之后,根据全局数组ids,设置需要选中的列
function dataGridSelection() {
if (!ids) {
return false;
}
var dataGridRows = $('#dataGrid').jqGrid('getRowData');
//找到相同的id列,设置其为选中状态
for (var i = 0; i < dataGridRows.length; i++) {
for (var j = 0; j < ids.length; j++) {
if (dataGridRows[i].id == ids[j]) {
$("#dataGrid").setSelection(dataGridRows[i].id)
}
}
}
}
//将选中行的ID都加到数组 ids,
function addOneGridSelect(id, isSelect) {
if (isSelect) {
if (ids.indexOf(id) == -1) {
ids.push(id);
}
} else {
var indexTmp = ids.indexOf(id);
if (indexTmp != -1) {
for (var i = 0; i < ids.length; i++) {
if (ids[i] == id) {
ids.splice(i, 1);
}
}
}
}
}
function addAllGridSelect(rowIds, isSelect) {
debugger;
if (isSelect) {
for (var i = 0; i < rowIds.length; i++) {
if (ids.indexOf(rowIds[i]) == -1) {
ids.push(rowIds[i]);
}
}
} else {
for (var i = 0; i < rowIds.length; i++) {
var indexTmp = ids.indexOf(rowIds[i]);
if (indexTmp != -1) {
for (var j = 0; j < ids.length; j++) {
if (ids[j] == rowIds[i]) {
ids.splice(j, 1);
}
}
}
}
}
}