本文共 2778 字,大约阅读时间需要 9 分钟。
级联勾选子节点:
第一种:级联勾选子节点(父节点级联子节点)
步骤:1.在treegrid的onCheck(选中)事件中写上函数(级联勾选子节点):
onCheck : function(row){ t = $(this); opts = t.treegrid("options"); if (opts.checkOnSelect && opts.singleSelect) { return; } var idField = opts.idField, id = row[idField], children, checked, parent = row; //如果当前节点被勾选,那么勾选该节点的子节点 $.each(t.treegrid("getChildren", id), function (i, n) { if (!t.treegrid("isChecked", n[idField])) { t.treegrid("check", n[idField]); } });},
2.在onUncheck(取消选中)事件中写上函数(级联取消勾选子节点):
t = $(this); opts = t.treegrid("options"); if (opts.checkOnSelect && opts.singleSelect) { return; } var idField = opts.idField, id = row[idField], children, checked, parent = t.treegrid("getParent", id); $.each(t.treegrid("getChildren", id), function (i, n) { t.treegrid("uncheck", n[idField]);});
第二种:级联勾选父节点(子节点级联父节点)
onCheck事件:
onCheck : function(row){ t = $(this); opts = t.treegrid("options"); if (opts.checkOnSelect && opts.singleSelect) { return; } var idField = opts.idField, id = row[idField], children, checked, parent = t.treegrid("getParent", id); while (parent) { children = t.treegrid("getChildren", parent[idField]); checked = t.treegrid("getChecked"); if (!$.array.some(children, function (val) { return !$.array.contains(checked, val); })) { if (!t.treegrid("isChecked", parent[idField])) { t.treegrid("check", parent[idField]); } } parent = t.treegrid("getParent", parent[idField]); } $.each(t.treegrid("getChildren", id), function (i, n) { //如果子节点没有勾选,那么勾选子节点 if (!t.treegrid("isChecked", n[idField])) { t.treegrid("check", n[idField]); } });}
onUncheck事件:
onUncheck:function(row){ t = $(this); opts = t.treegrid("options"); if (opts.checkOnSelect && opts.singleSelect) { return; } var idField = opts.idField, id = row[idField], children, checked, parent = t.treegrid("getParent", id); while (parent) { children = t.treegrid("getChildren", parent[idField]); checked = t.treegrid("getChecked"); if (!$.array.some(children, function (val) { return $.array.contains(checked, val); })) { if (t.treegrid("isChecked", parent[idField])) { t.treegrid("uncheck", parent[idField]); } } parent = t.treegrid("getParent", parent[idField]); } $.each(t.treegrid("getChildren", id), function (i, n) { t.treegrid("uncheck", n[idField]); });}
转载地址:http://zawuo.baihongyu.com/