$(document).ready(function() {
$("#jqGrid").jqGrid({
url: 'data.json',
datatype: "json",
colModel: [
{ label: 'ID', name: 'ProductID', width: 45, key: true, hidden:true },
{ label: 'Category Name', name: 'CategoryName', width: 75 },
{ label: 'Product Name', name: 'ProductName', width: 90 },
{ label: 'Country', name: 'Country', width: 100 },
{ label: 'Price', name: 'Price', width: 80, sorttype: 'integer' },
// sorttype is used only if the data is loaded locally or loadonce is set to true
{ label: 'Quantity', name: 'Quantity', width: 80, sorttype: 'number' }
],
loadonce: true,
viewrecords: true,
width: 780,
height: 200,
rowNum: 20,
rowList: [20, 30, 50],
rownumbers: true,
rownumWidth: 25,
multiselect: true,
pager: "#jqGridPager"
});
});
function getSelectedRows() {
var grid = $("#jqGrid"),
rowData;
var rowKey = grid.getGridParam("selrow");
if (!rowKey)
alert("No rows are selected");
else {
var selectedIDs = grid.getGridParam("selarrrow");
var result = "";
for (var i = 0; i < selectedIDs.length; i++) {
rowData = grid.getLocalRow(selectedIDs[i]);
result += rowData.ProductID + ", ";
result += rowData.CategoryName + ", ";
result += rowData.ProductName + ", ";
result += rowData.Country + ", ";
result += rowData.Price + ", ";
result += rowData.Quantity + ", ";
}
alert(result);
}
}
所以我不想要这个......
result += rowData.ProductID+", " ;
result += rowData.CategoryName+", " ;
result += rowData.ProductName+", " ;
result += rowData.Country+", " ;
result += rowData.Price+", " ;
result += rowData.Quantity+", " ;
我想循环通过colModels而不是任何帮助?提前谢谢
答案 0 :(得分:1)
循环访问您的对象并按键访问值
您的rowData
是一个javascript对象。您可以通过循环键来获取每个值。
这对你有用
for (var i = 0; i < selectedIDs.length; i++) {
rowData = grid.getLocalRow(selectedIDs[i]);
for (var item in rowData ) { //loop the object for each key
result += rowData[ item ] + ', '; // access the data of that key
}
}