經過一番挖掘,我找到了一種使用原始帖子代碼和一些自己的代碼進行工作的方法。當點擊jqgrid Tree網格中的複選框時,這將使節點的子節點和節點的父節點保持同步。
beforeSelectRow: function (rowid, e) {
var $this = $(this),
isLeafName = $this.jqGrid("getGridParam", "treeReader").leaf_field,
localIdName = $this.jqGrid("getGridParam", "localReader").id,
localData,
state,
parentState,
parentChildren,
setChechedStateOfChildrenItems = function (children) {
$.each(children, function() {
$("#" + this[localIdName] + " input.itmchk").prop("checked", state);
updateSelectedNode(row_id, this[localIdName], state);
if (!this[isLeafName]) {
setChechedStateOfChildrenItems($this.jqGrid("getNodeChildren", this));
}
});
},
setCheckedStateofParentItems = function (parent) {
parentChildren = $this.jqGrid("getNodeChildren", parent);
var selectedChildren = 0;
$.each(parentChildren, function() {
if ($("#" + this.id + " input.itmchk").prop("checked")) {
selectedChildren += 1;
}
});
if (selectedChildren == 0) {
parentState = false;
} else {
parentState = true;
}
updateSelectedNode(row_id, parent.id, parentState);
$("#" + parent.id + " input.itmchk").prop("checked", parentState);
if (parent.parent != null) {
setCheckedStateofParentItems($this.jqGrid("getLocalRow", parent.parent), state)
}
}
if (e.target.nodeName === "INPUT" && $(e.target).hasClass("itmchk")) {
state = $(e.target).prop("checked");
localData = $this.jqGrid("getLocalRow", rowid);
setChechedStateOfChildrenItems($this.jqGrid("getNodeChildren", localData), state);
setCheckedStateofParentItems($this.jqGrid("getLocalRow", localData.parent), state)
updateSelectedNode(row_id, localData.id, state); //this does the checkbox we checked
}
},
信元格式化器部分:
{
name: 'name', width: 25, index: 'name', label: "ColumnName", labelAlign: "left",
formatter: function (cellvalue, options, rowObject) {
var result = "<input type='checkbox' class='itmchk' checked> " + $.jgrid.htmlEncode(cellvalue);
if (rowObject.selected == false) {
result = result.replace("checked", "");
}
return result;
}
}