using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace Bowin.Web.Controls.Mvc { public class Tree : BaseControl { public string ItemSourceUrl { get; set; } public bool? IsCheckTree { get; set; } public UrlHelper UrlHelper { get; set; } public bool? IsCascadeCheck { get; set; } public override string Render() { StringBuilder sb = new StringBuilder(); TagBuilder tabBuilder = new TagBuilder("ul"); tabBuilder.MergeAttributes(GetPropertyList()); if (this.Attributes != null) { tabBuilder.MergeAttributes(this.Attributes); } sb.Append(string.Format("", ID + "_hidden", Name)); sb.Append(tabBuilder.ToString(TagRenderMode.Normal)); return sb.ToString(); } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); if (!string.IsNullOrEmpty(this.ID)) { dictPropertyList.Add("id", this.ID); } dictPropertyList.Add("class", string.Format("easyui-tree {0}", (string.IsNullOrEmpty(CssClass) ? "" : CssClass))); var dictOptions = new Dictionary(); if (!string.IsNullOrEmpty(this.ItemSourceUrl)) { dictOptions.Add("url", "'" + UrlHelper.Content(ItemSourceUrl) + "'"); } else { throw new Exception("请输入获取树节点的数据源Url。"); } if (this.IsCheckTree.HasValue && this.IsCheckTree.Value) { dictOptions.Add("checkbox", "true"); } if (this.IsCascadeCheck.HasValue) { dictOptions.Add("cascadeCheck", this.IsCascadeCheck == true ? "true" : "false"); } if (!this.IsCascadeCheck.HasValue || !this.IsCascadeCheck.Value) { dictOptions.Add("onBeforeCheck", "CMSFunction.Tree.OnBeforeCheck"); } dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value)))); return dictPropertyList; } public static Tree CreateControl(HtmlHelper htmlHelper, TreeOptions treeOptions, IDictionary attributes = null) { Tree tree = new Tree(); if (attributes != null) { tree.Attributes = attributes; } treeOptions = treeOptions ?? new TreeOptions(); //tree.CssClass = string.IsNullOrWhiteSpace(treeOptions.CssClass) ? "easyui-accordion" : treeOptions.CssClass; tree.CssClass = treeOptions.CssClass; tree.ID = treeOptions.ID; tree.Name = treeOptions.Name; //tree.IconCls = treeOptions.IconCls; //tree.DateItemCss = string.IsNullOrWhiteSpace(treeOptions.DateItemCss) ? "easyui-tree" : treeOptions.DateItemCss; tree.ItemSourceUrl = treeOptions.ItemSourceUrl; tree.IsCheckTree = treeOptions.IsCheckTree; tree.IsCascadeCheck = treeOptions.IsCascadeCheck; tree.UrlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); return tree; } } public class TreeItem { public string id { get; set; } public string text { get; set; } public string iconCls { get; set; } public bool @checked { get; set; } public object attributes { get; set; } public List children { get; set; } } }