2016-04-26 44 views
1

傢伙我使用的jqGrid ..我想遍歷colModels名來獲取行數據。這裏是我的代碼遍歷的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

遍歷你的對象和獲取價值的關鍵

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 
}   
} 
工作