在Easy UI tree操作中,遇到了一系列问题
1.点击树传参数
$('#featureTree').tree({ url: 'GetFeatureTree?guid=', method: 'get', checkbox:true, animate: true, dnd: true, onBeforeLoad: function (node, param) { //加载树前参数赋值参数 var guid = $('.roleGuid').val(); $('#featureTree').tree('options').url = "GetFeatureTree?guid=" + guid; }, });
2. 能够拖动排序
$(function () { $('#tree').tree({ url: 'GetDepartmentTree', method: 'get', animate: true, dnd: true, onClick: function (node) {//选中节点触发 loadDepartment(node.id); }, //拖动结束前触发 onBeforeDrop: function (target, source, point) { //point: 'append','top' or 'bottom'.表示目标内上下 //target: DOM object, The node being targeted for the drop.目标元素 //source: the source node.拖动节点 if (point != 'append') { var place; var targetNode = $(this).tree('getNode', target); //获取位置 if (point == 'top') { place = 0; } else//'bottom' { place = 1; } var startGuid = source.id; var endGuid = targetNode.id; $.ajax({ url: "DropTree", type: "POST", data: { "point": place, "startGuid": startGuid, "endGuid": endGuid }, datatype: "text", success: function (result) { if (result) { return true; } else { return false; } }, error: function () { return false; } }) } else { $.messager.alert("提示", "不允许此操作"); return false; } } }); })
3.后台递归算法(自动填写打勾)
#region 权限树 public ContentResult GetFeatureTree(Guid? guid) { List<Feature> lisFeature = new List<Feature>(); List<RoleFeature> lisRoleFeature = new List<RoleFeature>(); FeatureArgs fArg = new FeatureArgs(); if(guid != null) { lisRoleFeature = _RoleDAL.GetRoleFeature(guid.Value); } lisFeature = _FeatureDAL.GetFeatures(fArg); var nodestr = "{{\"id\":\"{0}\" ,\"text\": \"{1}\" ,\"checked\":{2},\"children\":["; var jsonstr = "["; if (lisFeature.Count > 0) { var rootNameList = lisFeature.Where(p => p.ParentGuid == null).ToList(); foreach (var item in rootNameList) { loadNodeOfFeature(lisFeature, item, nodestr, ref jsonstr, lisRoleFeature); } jsonstr = jsonstr.TrimEnd(','); jsonstr = jsonstr.TrimEnd('}'); jsonstr = jsonstr.TrimEnd(']'); jsonstr = jsonstr.TrimEnd(','); jsonstr += "]},"; } else { jsonstr += "{}]"; } jsonstr = jsonstr.TrimEnd(','); jsonstr += "]"; return Content(jsonstr); } private void loadNodeOfFeature(List<Feature> lisFeature, Feature feature, string nodestr, ref string jsonstr, List<RoleFeature> lisRoleFeature) { string isChecked = "false"; //判断是否需要打勾 if (lisRoleFeature.Any() && lisRoleFeature.Any(o => o.FeatureGuid == feature.Guid)) { isChecked = "true"; } //增加子节点 jsonstr += string.Format(nodestr, feature.Guid, feature.Title, isChecked); //判断节点是否有子节点 var nodeNameList = lisFeature.Where(p => p.ParentGuid == feature.Guid).OrderBy(p => p.SortNo).ToList(); if (nodeNameList.Any()) { foreach (var item in nodeNameList) { loadNodeOfFeature(lisFeature, item, nodestr, ref jsonstr, lisRoleFeature); } jsonstr = jsonstr.TrimEnd(','); jsonstr += "]},"; } else { jsonstr += "]},"; } } #endregion