123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- sb.Append(string.Format("<input type=hidden id={0} name={1}>", ID + "_hidden", Name));
- sb.Append(tabBuilder.ToString(TagRenderMode.Normal));
- return sb.ToString();
- }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- 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<string, string>();
- 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<string, string> 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<TreeItem> children { get; set; }
- }
- }
|