// see: https://free-jqgrid.github.io/getting-started/
// CDN used: https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid
$(function() {
$("#grid").jqGrid({
autowidth: true, height: 45,
colNames: ['First name', 'Last name', 'Updated?'],
colModel: [{name: "firstName"}, {name: "lastName"}, {name: "updated"}],
data: [
{ id: 10, firstName: "Jane", lastName: "Doe", updated: "no"},
{ id: 20, firstName: "Justin", lastName: "Time", updated: "no" }
],
loadComplete: function() {
// demo - how to access grid data
var ids = $(this).jqGrid('getDataIDs'); // ids for each row
var gridData = $(this).jqGrid('getGridParam', 'data'); // all rows
var gridLen = gridData.length; // number of rows
// now get a list from the data
var nameList = [];
for(var i=0; i<gridLen; i++) {
nameList.push(gridData[i].firstName+' [id:'+ ids[i] +']');
}
var strList = nameList.join(", ");
$("#nameList").html(strList);
var rowKey = ids[1]; // rowKey for the operations below
// get data from the 2nd row
var secondRow=$(this).jqGrid('getRowData', rowKey);
$("#getRowData").html(secondRow.firstName + ', ' + secondRow.lastName
+ ', updated:' + secondRow.updated);
// set update flag by modifying row data
secondRow.updated = "yes";
$(this).jqGrid('setRowData', rowKey, secondRow);
// update single cell (read, modify, write)
var lastName=$(this).jqGrid('getCell', rowKey, "lastName");
lastName=lastName.toUpperCase();
// first change the cell in the visible part of grid
$(this).jqGrid('setCell', rowKey, "lastName", lastName);
// now change the internal local data
$(this).jqGrid('getLocalRow', rowKey).lastName = lastName;
// make column label of "Updated?" column larger
$(this).jqGrid('setLabel', 2, '<h3>Updated?</h3>');
// --- now verify the changes ---
gridData = $(this).jqGrid('getGridParam', 'data'); // get rows again
var rowList = [];
for(var i=0; i<gridLen; i++) {
rowList.push(gridData[i].firstName + ', ' + gridData[i].lastName
+ ', updated:' + gridData[i].updated);
}
$("#showGridData").html(rowList.join("; "));
}
});
});
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>Canonical jqGrid example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/css/ui.jqgrid.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/jquery.jqgrid.min.js"></script>
<table id="grid"></table>
<br/><b>First names list:</b><div id="nameList"></div>
<br/><b>Get data from row#1 (before update):</b><div id="getRowData"></div>
<br/><b>Verify changes (after update):</b><div id="showGridData"></div>
你可以刪除'GridView控件:TRUE','autoencode:TRUE'和'寬度:你的例子中'100%'選項。 'gridview:true'在自由jqGrid中是默認的,因爲很長一段時間,自4.15.0以來'autoencode:true'。因爲您使用了'autowidth:true',所以選項'width:'100%'' 不需要。 – Oleg 2017-11-16 14:02:32
@ Oleg - 感謝您的提示,我已經更新了它。 – Matt 2017-11-16 14:39:16