为什么我不能将数组分配给jqgrid列?
我编写了以下代码以从数组生成列和列模型,有趣的是,当我在jqgrid中对生成的数组进行硬编码(如下所示)时,它会生成列,但是将数组放入变量中然后将其分配给colModel它不会生成列.为什么会这样,我该如何解决?
i have wrriten the following code to generate columns and column model from an array, whats funny is when i hard code the generated array in jqgrid like below it generates teh columns but when put the array to a variable and then assign it to colModel it wont generate the columns. why is that and how do i fix this?
完整的代码在底部
通过 http://www.trirand.com/blog/jqgrid/使用JQGrid jqgrid.html
" https://cdnjs .cloudflare.com/ajax/libs/jqgrid/4.6.0/js/jquery.jqGrid.min.js "
以下是硬编码代码:
//hard coding the array
$('#myGrid').jqGrid({
datatype: "local",
height: 250,
//colNames:modelArr,
colModel:[{name:' 9/10/2014'}, {name:' 1/1/2014'}, {name:' 2/10/2014'}],
viewrecords: true,
multiselect: true,
caption: "Manipulating Array Data"
});
这是完整的代码:
var colsArray =[];
var modelArr = [];
$.ajax({
url:'http://localhost:53721/Home/GetPurchases',
dataType:'json',
success:function(data){
colsArray=data;
var str='';
if(data.length > 0 ){
str = data[0].PurchaseDates;
console.log(str);
colsArray = str.split(',');
}
for(var i=0; i < colsArray.length;i++){
var tmp = {name:colsArray[i]};
modelArr[i]=tmp;
}
//console.log(JSON.stringify(modelArr));
},
error:function(){}
});
var d1 = JSON.stringify(modelArr);
console.log(d1);
$('#myGrid').jqGrid({
datatype: "local",
height: 250,
//colNames:modelArr,
colModel:[modelArr], //[{name:' 9/10/2014'}, {name:' 1/1/2014'}, {name:' 2/10/2014'}],
viewrecords: true,
multiselect: true,
caption: "Manipulating Array Data"
});
在将colModel分配给数组时,请删除方括号,因为保留它会产生数组数组.
remove the square brackets when you assign the colModel to an array, because keeping it will produce an array of array.
var myGrid = $("#myGrid").jqGrid({
datatype: "local",
height: 250,
colModel:modelArr,
viewrecords: true,
multiselect: true,
caption: "Manipulating Array Data"
});
这是使用您提供的代码的有效的示例.
我还在示例的末尾添加了两行,以显示两个分配形状之间的差异,如下图所示
here's a working sample using your provided code.
I also added two lines at the end of the sample to show the difference between the two assignment shapes as shown in the below image