我試圖創建本地表jqGrid的,這是所有偉大的工作,直到林改變陣列,這裏是工作代碼:
HTML代碼:
<div id=main style="width: 350px; height:200px;background-color:orange;">
<table id="grid"></table>
</div>
JS代碼
$(document).ready(
function()
{
jQuery("#grid").jqGrid({
datatype: 'local',
colNames:["User Name","Meet","Time"],
height:"100%",
autowidth: true,
colModel :[
{name:"name",index:"name"},
{name:"to_meet",index:"to_meet"},
{name:"time",index:"time"}
],
gridview: true,
viewrecords: true });
});
var mydata = [
{name:"test",to_meet:"111",time:"0500"},
{name:"test2",to_meet:"112",time:"0530"},
{name:"test3",to_meet:"113",time:"0600"},
{name:"test4",to_meet:"114",time:"0630"},
{name:"test5",to_meet:"115",time:"0700"},
{name:"test6",to_meet:"116",time:"0730"},
{name:"test7",to_meet:"117",time:"0800"},
{name:"test8",to_meet:"118",time:"0830"},
{name:"test9",to_meet:"119",time:"0900"},
{name:"test10",to_meet:"120",time:"0930"},
{name:"test11",to_meet:"121",time:"1000"},
{name:"test12",to_meet:"122",time:"1030"},
{name:"test13",to_meet:"123",time:"1100"},
{name:"test14",to_meet:"124",time:"1130"},
{name:"test15",to_meet:"125",time:"1200"},
{name:"test16",to_meet:"126",time:"1230"}
];
console.log(mydata.length)
for(var i=0; i<mydata.length;i++)
{
alert(mydata);
jQuery("#grid").jqGrid('addRowData',i+1,mydata[i]); ;
}
您也可以從這裏運行: http://jsfiddle.net/bYQn6/2/
現在,當我改變obj的數組:
$.each(myarray, function(i, val){
gridi.push('{name:"' + val.Name + '",to_meet:"' + val.meet + '",time:"' + val.time + '"}');
});
var mydata = [gridi.toString()];
印刷本「MYDATA」將長相酷似「MYDATA」上面,但它不工作:(
我在做什麼錯?
謝謝!!!
您應該1)在創建jqGrid之前移動mydata的定義* 2)爲'mydata'的每個元素添加'id'屬性; 3)向jqGrid添加'data:mydata'選項。 4)你應該刪除你調用addRowData **的循環。順便說一下,如果你的數據沒有本地'id',我會建議你通過調用函數'$ .jgrid.randId()'初始化項目的id屬性。即使網頁上有更多的一個網格,網格的行也會確保獲得*唯一*值。 – Oleg