// 初始化DataGrid對象
$('#dataGrid').dataGrid({
searchForm: $("#searchForm"),
showCheckbox: true,
columnModel: [
{header:'${text("主鍵")}', name:'id', index:'a.id', width:150, align:"center",hidden:true},
//code作為回調(diào)頁面保存使用,value僅作為回調(diào)頁面的展示使用
{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("拍攝產(chǎn)品code")}', name:'categoryProduct', index:'a.category_product', width:150, align:"center",hidden:true},
{header:'${text("拍攝產(chǎn)品")}', 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();
},
// 加載成功后執(zhí)行事件
ajaxSuccess: function(data){
}
});
再添加以下三個方法
//表格加載完之后,根據(jù)全局?jǐn)?shù)組ids,設(shè)置需要選中的列
function dataGridSelection() {
if (!ids) {
return false;
}
var dataGridRows = $('#dataGrid').jqGrid('getRowData');
//找到相同的id列,設(shè)置其為選中狀態(tài)
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都加到數(shù)組 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);
}
}
}
}
}
}