gpt4 book ai didi

javascript - 循环遍历 jqGrid 中的 colModels 名称

转载 作者:行者123 更新时间:2023-11-27 23:03:15 24 4
gpt4 key购买 nike

伙计们,我正在使用 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 ..有什么帮助吗?提前致谢

最佳答案

Loop through your Object and access values by key

您的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
}
}

关于javascript - 循环遍历 jqGrid 中的 colModels 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36859699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com