easyui-tree根据叶子节点获取父节点值(N层)

来源:转载

easyui-tree根据叶子节点获取父节点值(N层)

思路:将叶子节点的id值用字符串拼接,如:叶子节点id=“根节点id-父节点id-叶子节点id”,用“-”间隔来取值。

easyui-tree根据叶子节点获取父节点值(N层)

html:

<div class="easyui-panel" style="padding:5px;height: 840px;"> <ul id="myTree" class="easyui-tree"></ul> </div>

easyui-tree根据叶子节点获取父节点值(N层)

jQuery:

 <script type="text/javascript"> //加载tree function LoadTree() { $.ajax({ type: "GET", url: "/Rights/Job/GetTreeJson", success: function (result) { var myJson = eval('(' + result + ')'); $("#myTree").tree({ data: myJson, onlyLeafCheck: false,//仅叶子节点可以被选中 checkbox: false,//开启多选框 }); } }); } //初始化 $(function () { LoadTree(); }); //点击左侧列表 $(function () { $("#DataGrid").datagrid({ onClickRow: function (index, row) { //var JobID = row.JobID; if ($("#divTabs").tabs("exists", "范围设置")) { $("#divTabs").tabs("select", "范围设置"); } CheckTree(); } }); }); //tree编辑的按钮 $(function () { $("#btn_FW_Edit").click(function () { var rows = $("#DataGrid").datagrid("getSelections"); if (rows.length != 1) { alert("请左边列表中选择一个[岗位]!"); return; } else { CheckTree(); } }); }); function CheckTree() { $("#div_FW_Edit").hide(); $("#div_FW_Save").show(); $("#myTree").tree({ //onlyLeafCheck: true,//仅叶子节点可以被选中 checkbox: true,//开启多选框 }); //先将回显数据全部清除 var root = $("#myTree").tree("getRoot"); $("#myTree").tree("uncheck", root.target); var rows = $("#DataGrid").datagrid("getSelections"); var id = rows[0].JobID; $.ajax({ url: "/Rights/JobViewRange/BindTree", data: { JobID: id, }, success: function (data) { for (x in data) { var node = $("#myTree").tree("find", data[x].RangeValue); $("#myTree").tree("check", node.target); } } }); } //保存勾选的tree $(function () { $("#btn_FW_Save").click( function () { var rows = $("#DataGrid").datagrid("getSelections"); if (rows.length != 1) { alert("请左边列表中选择一个[岗位]!"); return; } else { var id = rows[0].JobID; var BrandID = rows[0].BrandID; //tree选中项目: var nodes = $("#myTree").tree("getChecked"); var res_id = ''; for (var i = 0; i < nodes.length; i++) { if (res_id != '') res_id += ','; res_id += nodes[i].id; } if (res_id == '') { alert("请选择[范围值]!"); return; } //alert(res_id); $.ajax({ type: "POST", url: "/Rights/JobViewRange/SaveTree", data: { JobID: id, RangeValue: res_id, BrandID: BrandID, }, success: function (data) { if (data == "OK") { alert("修改成功!"); LoadTree(); //document.getElementById('frameExecute').contentWindow.location.reload(true); } else { alert(data); } } }); } }); }); </script>

easyui-tree根据叶子节点获取父节点值(N层)

MVC:

public ActionResult SaveTree() { UserInformation user = RequestUserSession.GetSessionUser(); int JobID = 0; int.TryParse(Request["JobID"], out JobID); int BrandID = 0; int.TryParse(Request["BrandID"], out BrandID); string RangeValueTemp = Request["RangeValue"]; if (JobID == 0) return Content("保存失败"); if (string.IsNullOrEmpty(RangeValueTemp)) return Content("保存失败"); string[] arrTemp = RangeValueTemp.Split(','); List<JobViewRange> jvrList = new List<JobViewRange>(); if (arrTemp != null) { foreach (var temp in arrTemp) { if (temp.Length > 3 && temp.Contains('-')) { string[] arrValue = temp.Split('-'); if (arrValue != null && arrValue.Length == 4) { var jvr = jvrList.Find(a => a.JobID == JobID && a.ModuleID == int.Parse(arrValue[0].ToString()) && a.MenuID == int.Parse(arrValue[1].ToString()) && a.RangeType == arrValue[2].ToString() ); var jvrIndex = jvrList.FindIndex(a => a.JobID == JobID && a.ModuleID == int.Parse(arrValue[0].ToString()) && a.MenuID == int.Parse(arrValue[1].ToString()) && a.RangeType == arrValue[2].ToString() ); if (jvr != null) { jvr.RangeValue = jvr.RangeValue + "," + arrValue[3];//用,逗号拼接RangeValue值 jvrList.RemoveAt(jvrIndex);//移除旧的 jvrList.Add(jvr);//添加新的 } else { JobViewRange job = new JobViewRange(); job.JobID = JobID; job.ModuleID = int.Parse(arrValue[0].ToString()); job.MenuID = int.Parse(arrValue[1].ToString()); job.RangeType = arrValue[2]; job.RangeValue = arrValue[3]; job.BrandID = BrandID; job.CreateBy = user.UserName; job.CreateDate = DateTime.Now; job.UpdateBy = user.UserName; job.UpdateDate = DateTime.Now; jvrList.Add(job); } } } } if (jvrList.Any()) { foreach (var job in jvrList) { var b = JobViewRangeService.SaveTree(job); } return Content("OK"); } } return Content("保存失败"); } public ActionResult BindTree() { int JobID = 0; int.TryParse(Request["JobID"], out JobID); if (JobID == 0) return null; List<JobViewRange> tempList = JobViewRangeService.BindTree(JobID); if (tempList != null) { List<JobViewRange> list = new List<JobViewRange>(); foreach (var temp in tempList) { string[] arr = temp.RangeValue.Split(','); foreach (var RangeValue in arr) { JobViewRange job = new JobViewRange(); job.RangeValue = temp.ModuleID + "-" + temp.MenuID + "-" + temp.RangeType + "-" + RangeValue; list.Add(job); } } return Json(list, JsonRequestBehavior.AllowGet); } return null; }

easyui-tree根据叶子节点获取父节点值(N层)

json:

public string GetTreeJson() { var module = GetModule(); if (module != null && module.Any()) { string jsonData = "["; module.ForEach(a => { jsonData += "{"; jsonData += "/"id/":/"" + a.ModuleID + "/","; jsonData += "/"text/":/"" + a.ModuleName + "/","; jsonData += "/"children/":["; var menu = GetMenu(a.ModuleID); menu.ForEach(b => { jsonData += "{"; jsonData += "/"id/":/"" + b.MenuID + "/","; jsonData += "/"text/":/"" + b.MenuName + "/","; jsonData += "/"children/":["; var rangeType = GetRangeType(b.MenuID); rangeType.ForEach(c => { jsonData += "{"; jsonData += "/"id/":/"" + c.RangeType + "/","; jsonData += "/"text/":/"" + c.RangeTypeName + "/","; jsonData += "/"children/":["; var rangeValue = GetRangeValue(b.MenuID, c.RangeType); rangeValue.ForEach(d => { jsonData += "{"; //jsonData += "/"id/":/"" + d.Value + "/","; jsonData += "/"id/":/"" + a.ModuleID + "-" + b.MenuID + "-" + c.RangeType + "-" + d.Value + "/",";//模块id-菜单id-范围类型-范围值id 如:1-1-T_Brand-1;就能根据此叶子节点id值split('-') jsonData += "/"text/":/"" + d.Text + "/""; jsonData += "}"; jsonData += ","; }); jsonData = jsonData.TrimEnd(','); jsonData += "]}"; jsonData += ','; }); jsonData = jsonData.TrimEnd(','); jsonData += "]}"; jsonData += ','; }); jsonData = jsonData.TrimEnd(','); jsonData += "]}"; jsonData += ','; }); jsonData = jsonData.Substring(0, jsonData.Length - 1); jsonData += "]"; return jsonData; } return ""; }
easyui-tree根据叶子节点获取父节点值(N层)










分享给朋友:
您可能感兴趣的文章:
随机阅读: