EasyUI组合树插件

一、引用CSS和JS

<link href="~js/easyui/easyui.css" rel="stylesheet" type="text/css" />
<script src="~js/easyui/jquery.easyui.min.js" type="text/javascript"></script>

二、HTML代码

<input type="text" id="subject" name="subject">

三、JS代码

$('#dept').combotree({
    url: '#{GetDeptTree}',
    required: false,
    onSelect: function (node) {
        $.ajax({
            type: "POST",
            url: "#{GetJobs}",
            data: "deptId=" + node.id,
            success: function (data) {
                $("#job").html(data);
            }
        });
    }
});

$('#dept').combotree('setValue', "#{employmentNeeds.Dept.Id}");

 

//部门员工树
$('#Employee').combotree({
    url: '#{GetEmployees}',
    required: false,
    onSelect: function (node) {
        //控制只能选叶子节点
        //返回树对象  
        var tree = $(this).tree;
        //选中的节点是否为叶子节点,如果不是叶子节点,清除选中  
        var isLeaf = tree('isLeaf', node.target);
        if (!isLeaf) {
            //清除选中  
            $('#Employee').combotree('clear');
            return;
        }

        //员工基本信息赋值
        var att = eval("({" + node.attributes + "})");
        $("#spanDeptName").html(att.DeptName);
        $("#spanJobName").html(att.JobName);
        $("#spanEmpCode").html(att.EmpCode);
        $("#spanEntryTime").html(att.EntryTime);
    }
});

三、后台代码:

/// <summary>
/// 获取员工(部门树和员工)
/// </summary>
public void GetEmployees()
{
    echoJson(GetDepts(Constants.OptionAllVal, dropListService.GetDepts(LoginUser)));
}

/// <summary>
/// 获取部门树
/// </summary>
private List<Dictionary<string, object>> GetDepts(int parentDeptId, List<IMP_Dept> allDeptList)
{
    List<Dictionary<string, object>> dicList = new List<Dictionary<string, object>>();

    List<IMP_Dept> deptList = allDeptList.FindAll(a => a.PId == parentDeptId);
    if (deptList.Count == 0 && parentDeptId != Constants.OptionAllVal) return null;

    foreach (IMP_Dept dept in deptList)
    {
        Dictionary<string, object> dic = new Dictionary<string, object>();
        dic.Add("id", dept.Id);
        dic.Add("text", dept.Name);
        dic.Add("checkbox", true);

        List<Dictionary<string, object>> childDicList = new List<Dictionary<string, object>>();

        //当前部门下的子部门
        List<Dictionary<string, object>> childDeptDicList = GetDepts(dept.Id, allDeptList);
        if (childDeptDicList != null)
        {
            childDicList.AddRange(childDeptDicList);
        }

        //当前部门下的员工
        List<Dictionary<string, object>> childEmployeeDicList = GetEmployees(dept);
        if (childEmployeeDicList != null)
        {
            childDicList.AddRange(childEmployeeDicList);
        }

        if (childDicList != null)
        {
            dic.Add("state", parentDeptId == Constants.OptionAllVal ? "open" : "closed");
            dic.Add("children", childDicList);
        }

        dicList.Add(dic);
    }

    return dicList;
}

/// <summary>
/// 获取部门下的员工
/// </summary>
private List<Dictionary<string, object>> GetEmployees(IMP_Dept dept)
{
    List<Dictionary<string, object>> dicList = new List<Dictionary<string, object>>();

    List<IMP_Employee> employeeList = employeeService.FindEmployeeDept(dept.Id);
    if (employeeList.Count == 0) return null;

    foreach (IMP_Employee employee in employeeList)
    {
        Dictionary<string, object> dic = new Dictionary<string, object>();
        dic.Add("id", "emp" + employee.Id);
        dic.Add("text", employee.Name);
        dic.Add("checkbox", true);
        string attributes = string.Format("'DeptName':'{0}','JobName':'{1}','EmpCode':'{2}','EntryTime':'{3}'",
            employee.Dept == null ? "" : employee.Dept.Name,
            employee.Job == null ? "" : employee.Job.Name,
            employee.Code == null ? "" : employee.Code,
            employee.EntryTime == DateTime.MinValue ? "" : employee.EntryTime.ToString(Constants.FormatDate));
        dic.Add("attributes", attributes);
        dicList.Add(dic);
    }

    return dicList;
}

 

 

方法

扩展的方法继承于 combo,下面是添加和覆盖方法combotree.

名称参数说明
optionsnone返回选择对象。
treenone返回对象的树。下面的例子显示了如何获得选中的树节点。
var t = $('#cc').combotree('tree');	// 获得树对象
var n = t.tree('getSelected');		// 获得选中节点显示(文本);
loadDatadata加载本地组合树数据。代码示例:
$('#cc').combotree('loadData', [{id: 1,text: 'Languages',children: [{id: 11,text:'Java'},{id: 12,text: 'C++'}]}]);
reloadurl再次请求远程树数据。通过“url”参数覆盖原始url值。
clearnone清除的组件值。
setValuesvalues设置组件值数组。代码示例:
$('#cc').combotree('setValues', [1,3,21]);
setValuevalue

设置组件的值。代码示例:

$('#cc').combotree('setValue', 6);

 

 

参考:http://blog.csdn.net/magister_feng/article/details/6619870

 

自定义属性:http://www.cnblogs.com/Nature-j/archive/2013/05/06/3062598.html

转载于:https://www.cnblogs.com/s0611163/p/3749462.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值