C#+HTML+JS生成的树完整代码

VIEW层代码:

 1:  <script src="/Scripts/JQTree/jquery.tree.js" type="text/javascript"></script>

 2:  

 3:   <script src="/Scripts/JQTree/plugins/jquery.tree.checkbox.js" type="text/javascript"></script>

 4:  

 5:   <script>

 6:  $(function() {

 7:           var _CategoryTree = {

 8:               OpenPCategoryTree: function() {

 9:                   $.tree.reference("#PCategoryTree").open_all();

10:               },

11:               ClosePCategoryTree: function() {

12:                   $.tree.reference("#PCategoryTree").close_all();

13:               },

14:               CreatePCategoryTree: function() {

15:                   var values = {};

16:                   $("#PCategoryTree").tree({

17:                       types: {

18:                           "default": {

19:                               draggable: false

20:  }

21:                       },

22:                       ui: {

23:                           animation: 150,

24:                           theme_name: "checkbox"

25:  },

26:                       plugins: {

27:                           checkbox: {}

28:                       },

29:                       callback: {

30:                           onsearch: function(n, t) {

31:                               $.each(n, function(i, o) {

32:                                   $(o).addClass('search');

33:                               })

34:                           }

35:                       }

36:                   });

37:               }

38:           }

39:           _CategoryTree.CreatePCategoryTree();

40:           _CategoryTree.OpenPCategoryTree();

41:   

42:           $("#PCategoryTree").find("a").each(function() {

43:               $(this).click(function() {

44:                   $("#ParentID").val($(this).attr("value"));

45:                   $("#PCategoryTree").find("span").removeClass("hover");

46:                   $(this).closest("span").addClass("hover");

47:               });

48:           });

49:   

50:       });

51:   </script>

controller层代码:

 1:  public ActionResult TreeDepartment()

 2:          {

 3:   

 4:              Entity.OA.Department department = Service.DepartmentsService.GetTree();

 5:              StringBuilder html = new StringBuilder();

 6:              html.Append("<ul id='TreeList'>");

 7:              this.GetSubs(html, department);

 8:              html.Append("</ul>");

 9:              ViewData["ProductCategoryData"] = html.ToString();

10:              return View();

11:          }

12:   

13:          /// <summary>

14:          /// 通过递归拼树形结构

15:  /// </summary>

16:          /// <param name="html">树形Html字符串</param>

17:          /// <param name="category">树形商品分类对象</param>

18:  private void GetSubs(StringBuilder html, Entity.OA.Department department)

19:          {

20:              html.Append("<li  id='" + department.DeptID + "' name='" + department.DeptName + "'>");

21:              html.Append("<span><input type='hidden' value='" + department.DeptID + "' />");

22:              //html.Append("<ins>&nbsp;</ins>"); //产生复选框

23:  html.Append("<a href='javascript:void(0)' id='" + department.DeptName + "'value='" + department.DeptID + "'>");

24:              html.Append(department.DeptName);

25:              html.Append("</a></span>");

26:              if (department.SubList != null && department.SubList.Count > 0)

27:              {

28:                  html.Append("<ul>");

29:                  foreach (var item in department.SubList)

30:                  {

31:                      this.GetSubs(html, item);

32:                  }

33:                  html.Append("</ul>");

34:              }

35:              html.Append("</li>");

36:          }

service层代码 :

 1:  static Data.OA.IDepartmentsRepository iDepartmentsRepository = new Data.OA.DepartmentsRepository();

 2:   

 3:  static List<Entity.OA.Department> entitiesList = null;

 4:   

 5:  internal static void Reload()

 6:  {

 7:      entitiesList = (from pc in iDepartmentsRepository.GetDepartments()

 8:                      orderby pc.DeptName ascending

 9:                      select pc).ToList();

10:  }

11:   

12:  #region 树型部门列表

13:  /// <summary>

14:  /// 虚拟产品类别 

15:  /// </summary>

16:  /// <returns>虚拟产品类别列表</returns>

17:  public static Entity.OA.Department GetTree()

18:  {

19:      Reload();

20:      Entity.OA.Department root = new Entity.OA.Department();

21:      try

22:  {

23:          root = entitiesList.Single(

24:             item => item.DeptID.Equals(Entity.OA.Department.ROOTID));//获取跟节点

25:  GetSubs(root);

26:   

27:      }

28:      catch (InvalidOperationException ex)

29:      {

30:          ex.ToString();

31:      }

32:      return root;

33:  }

34:   

35:   

36:  /// <summary>

37:  /// 根据父对象,找到子孙树

38:  /// </summary>

39:  /// <param name="father">父对象</param>

40:  static public void GetSubs(Entity.OA.Department father)

41:  {

42:      father.SubList = entitiesList.Where(item =>

43:          item.ParentID.Equals(father.DeptID)).ToList();

44:      father.SubList.ForEach(item =>

45:      {

46:          item.Father = father;

47:          GetSubs(item);      

48:      });

49:   

50:  }

51:  #endregion
posted @ 2011-05-20 17:07  张占岭  阅读(1879)  评论(1编辑  收藏  举报