循环遍历jqGrid中的colModels名称

时间:2016-04-26 08:43:00

标签: javascript jquery jqgrid

我正在使用jqGrid ..我想循环遍历colModels名称以获取行数据..这是我的代码

$(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而不是任何帮助?提前谢谢

1 个答案:

答案 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
 }        
}