0
所以,我使用jqGrid,由於這個項目的性質,我需要動態地構建出colModel。意思是,我需要動態地建立模型本身。我正在用我的JSON調用檢索數據。 因此,我建立一個對象數組,然後將該數組分配給colModel屬性。沒有錯誤,但數據不顯示....我正在做一些非常類似於colNames的東西,它工作正常。有人看到我失蹤了嗎?我昨天下午和今天早上一直在做這件事,並且找不到任何不應該起作用的理由。正如你所看到的,它將我的代碼頂部的siteVal數組賦給colModel屬性。jqGrid中動態構建的colModel有什麼問題?
var siteVal = [{name: 'InvtId', index: 'InvtId', width: 20, editable: false, sortable: false, align: 'left', hidden: true}];
siteVal.push({name: 'Descr', index: 'Descr', width: 320, sortable: false, editable: false, align: 'left'});
siteId.forEach(function(site){
curSite = site.substr(0,1)+"Val";
siteVal.push({name: curSite, index:curSite, width: 20, editable: false, sortable: false, align: 'left', hidden: true});
})
siteVal.push({name: 'Qty', index: 'Qty', width: 100, editable: true, sortable: false, align: 'right', hidden: true});
var colData = ['', 'Description'];
colData = colData.concat(siteId);
colData = colData.concat('Quantity');
console.log(colData);
jQuery("#list3").jqGrid({
url: 'OrdersInput.php?do=getdelvprice&state=' + $("#State").val() + '&city=' + $("#City").val() + '&FType=' + $("#FType").val() + '&siteid=' + $("#Plant").val(),
datatype: 'json',
mtype: 'GET',
colNames: colData,
colModel: siteVal,
loadonce: true,
height: 525,
width: 605,
rowNum: 1000,
key: false,
cellEdit: true,
cellsubmit: 'clientArray',
gridComplete: function() {
$("#MsgDel2").html("");
}
});
在我使用console.log在構建它後立即查看數組時,這就是我所得到的。
{"name":"InvtId","index":"InvtId","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"Descr","index":"Descr","width":320,"sortable":false,"editable":false,"align":"left"},
{"name":"TVal","index":"TVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"MVal","index":"MVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"PVal","index":"PVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"DVal","index":"DVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"WVal","index":"WVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"BVal","index":"BVal","width":20,"editable":false,"sortable":false,"align":"left","hidden":true},
{"name":"Qty","index":"Qty","width":100,"editable":true,"sortable":false,"align":"right","hidden":true}
這看起來和我認爲應該看起來完全一樣,它完全模仿了靜態定義colModel的現有代碼。事情是,這必須是動態的,以應對未來的增長......
您在瀏覽器的控制檯中是否收到錯誤消息? –
根本沒有。這是什麼讓我如此困惑..它似乎會顯示一個錯誤,如果它不加載.. – trouble706
首先你應該總是寫jqGrid的哪個分支([免費jqGrid](https://github.com)/free-jqgrid/jqGrid),[Guriddo jqGrid JS](http://guriddo.net/?page_id=103334)或版本<= 4.7中的舊jqGrid)以及您在測試中使用的版本。秒使用'siteId'生成'colModel',但不包含任何您使用的數據。 'name'屬性必須包含id規則(例如沒有空格)。在'colModel'中設置'index'不是必需的,不推薦使用,缺省值爲editable:false,sortable:false,align:'left''的屬性也可以跳過。 – Oleg