jquery通过递归为easyui生成无限级别的下拉树

该示例用jquery+easyui来生成下拉树的数据,通过ws传递一个datatable的json数据,然后将此数据在Js中生成easyui tree插件用的json数据,再加载到树上。

easyui的版本是jQuery EasyUI 1.2.5

获取第一级数据:

 

function BindTree() {
    var gov = $("#dropCity").val();
    var grade = $("#dropGrade").val();
    var subject = $("#dropSubject").val();
    var term = $("#dropTerm").val();
    var edition = $("#dropEdition").val();
    var week = $("#hidCurWeek").val();
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "Handler/SysWeekSet.aspx/GetUnitList",
        data: "{gov:'" + gov + "',grade:'" + grade + "',subject:'" + subject + "',term:'" + term + "',edition:'" + edition + "',week:'" + week + "'}",
        dataType: "json",
        success: function(data) {
            var list = eval(data.d);
            var jsonstr = "[";
            for (var i = 0; i < list.length; i++) {
                if (list[i].CODELEVEL == "1") {
                    if (jsonstr == "[")
                        jsonstr += "{\"id\":\"" + list[i].UNIT_CODE + "\",\"text\":\"" + list[i].UNIT_NAME + "\"" + BindChildNode(list, list[i].UNIT_CODE) + "}";
                    else
                        jsonstr += ",{\"id\":\"" + list[i].UNIT_CODE + "\",\"text\":\"" + list[i].UNIT_NAME + "\"" + BindChildNode(list, list[i].UNIT_CODE) + "}";
                }
            }
            jsonstr += "]";
            $('#treeUnit').tree("loadData", eval(jsonstr));
        },
        error: function() {
            $.messager.alert('提醒', '数据传输失败');
        }
    });
}

获取下级别数据并进行递归: 

function BindChildNode(source,parcode) {
    var result="";
    for (var i = 0; i < source.length; i++) { 
        if(source[i].PARENTCODE==parcode)
        {
            if(result=="")
            {
                result=",\"children\":[";
                result += "{\"id\":\"" + source[i].UNIT_CODE + "\",\"text\":\"" + source[i].UNIT_NAME + "\",\"checked\":" + source[i].ISSELECT + "" + BindChildNode(source, source[i].UNIT_CODE) + "}";
            }
            else
            {
                result += ",{\"id\":\"" + source[i].UNIT_CODE + "\",\"text\":\"" + source[i].UNIT_NAME + "\",\"checked\":" + source[i].ISSELECT + "" + BindChildNode(source, source[i].UNIT_CODE) + "}";
            }
        }
    }
    if(result!="")
        result +="]";
    return result;
}

posted on 2012-02-09 16:09  刑天  阅读(2364)  评论(0编辑  收藏  举报

导航