easyui的treegrid的級聯勾選子節點,或者級聯勾選父節點

天空夜空星發表於2018-08-24

級聯勾選子節點:

第一種:級聯勾選子節點(父節點級聯子節點)
步驟:

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]);
             });
}


相關文章