我已经阅读了这个线程并且我已经下载了 Oleg 的演示,但是在我的代码中它不起作用。
我有一个带有 Json 的外部 PHP 的 jqGrid。我使用 setinteval(1 秒)实时刷新数据,但是当我炸毁一棵树时,它会立即关闭。
我的 javascript 代码到 jQuery(document).ready(function(): `
// odd even row
var resetAltRows = function () {
$(this).children("tbody:first").children('tr.jqgrow').removeClass('odd');
$(this).children("tbody:first").children('tr.jqgrow:visible:odd').addClass('odd');
};
'use strict';
var $grid = $('#list'),
saveObjectInLocalStorage = function (storageItemName, object) {
if (typeof window.localStorage !== 'undefined') {
window.localStorage.setItem(storageItemName, JSON.stringify(object));
}
},
removeObjectFromLocalStorage = function (storageItemName) {
if (typeof window.localStorage !== 'undefined') {
window.localStorage.removeItem(storageItemName);
}
},
getObjectFromLocalStorage = function (storageItemName) {
if (typeof window.localStorage !== 'undefined') {
return JSON.parse(window.localStorage.getItem(storageItemName));
}
},
myColumnStateName = function (grid) {
return window.location.pathname + '#' + grid[0].id;
},
idsOfExpandedRows = [],
updateIdsOfExpandedRows = function (id, isExpanded) {
var index = $.inArray(id, idsOfExpandedRows);
if (!isExpanded && index >= 0) {
idsOfExpandedRows.splice(index, 1); // remove id from the list
} else if (index < 0) {
idsOfExpandedRows.push(id);
}
saveObjectInLocalStorage(myColumnStateName($grid), idsOfExpandedRows);
},
orgExpandRow = $.fn.jqGrid.expandRow,
orgCollapseRow = $.fn.jqGrid.collapseRow;
idsOfExpandedRows = getObjectFromLocalStorage(myColumnStateName($grid)) || [];
jQuery("#list").jqGrid({
url:'get_tree.php',
datatype: "json",
ajaxGridOptions: { contentType: "application/json" },
jsonReader: {
id: "id",
cell: "",
root: function (obj) { return obj.rows; },
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) { return obj.rows.length; },
repeatitems: true
},
beforeProcessing: function (data) {
var rows = data.rows, i, l = rows.length, row, index;
for (i = 0; i < l; i++) {
row = rows[i];
index = $.inArray(row[0], idsOfExpandedRows);
row[30] = index >= 0; // set expanded column
row[31] = true; // set loaded column
}
},
colNames:[...............],
colModel :[
{name:'id', index:'id', width:15, sortable: false, title: false,hidden: true},
{name:'0', index:'0', classes:'piu', width:15, sortable: false, title: false},
{..............}
],
rowNum:100,
viewrecords: true,
autowidth: true,
height: 'auto',
loadonce:true,
key: true,
loadComplete: function() {
var grid = this;
resetAltRows.call(this);
$(this).find('tr.jqgrow td div.treeclick').click(function(){
resetAltRows.call(grid);
});
$(this).find('tr.jqgrow td span.cell-wrapper').click(function(){
resetAltRows.call(grid);
});
},
gridview: true,
treeGrid: true,
treeGridModel: "adjacency",
ExpandColumn: '0',
ExpandColClick: true
});
$.jgrid.extend({
expandRow: function (rc) {
alert('before expandNode: rowid="' + rc._id_ + '", name="' + rc.name + '"');
updateIdsOfExpandedRows(rc._id_, true);
return orgExpandRow.call(this, rc);
},
collapseRow: function (rc) {
alert('before collapseNode: rowid="' + rc._id_ + '", name="' + rc.name + '"');
updateIdsOfExpandedRows(rc._id_, false);
return orgCollapseRow.call(this, rc);
}
});
//REFRESH JSON
setInterval(
function() {
jQuery("#list").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
}
, 1000);
`
我也改变了这个
row[30] = index >= 0;
row[31] = true;
具有json的“扩展”和“加载”字段的确切位置。