jquery easyui tree 异步加载数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//遇到的主要问题是,展开两次会重复加载,所以在属性中建立标志位,来看是否已经有了子节点(需要注意的是如果 添加了节点就要重新初始化了,否则如果有节点就不会再去请求了),<br>第二个困难时,无法刷新数据,附加上了节点,必须折叠再展开才行,代码需要加上<br>function initTree() {
              myUrl = "../../Organization/GetOrganizationTree";
              $.post(
                    myUrl,
                    {"isPost": 1},
                    function (data) {
                         
                        $("#mytree").tree({ 
                            data: data,
                            lines:true,
                            onClick : function(node) { 
                                $.cookie("level", node.attributes.level);
                                if (node.attributes.level == 4) {
                                    $.cookie("curNodeId", node.id.substring(0, node.id.indexOf(‘_‘)));
                                    $.cookie("curIsEm", node.attributes.isEm);
                                     
                                }
                                else if (node.attributes.level == 5) {
 
                                }
                                else {                                   
                                    $.cookie("curNodeId", node.id);
                                }
                            },
                            onBeforeExpand: function (node) {
                                if (node.attributes.level == 4) {
 
                                    //alert(node.id+" " + node.id.indexOf("_") +" "+ node.id.substring(0, node.id.indexOf("_") - 1));
                                    appendPost(node.id.substring(0, node.id.indexOf(‘_‘)),1, node, node.attributes.isEm);
                                }
                                else if (node.attributes.level == 5) {
                                    appendPost(node.id, 0, node, node.attributes.isEm);
 
                                }
                            }
 
                        });
                    },
                    "json");
 
 
               
          }         
 
          //添加(根据父角色查询岗位)
          function appendPost(parentId, isFirst, node, isEm) {
              $.post(
                     "../../PostManage/GetGeneralPostForTree",
                     {
                         "parentId": parentId,
                         "isFirst": isFirst,
                         "isEm": isEm
                     },//本层的是节点是 nodePostKind,orgId是上一层的node变量
                     function (jsonPost) {
                         //如果当前没有请求道数据
                         if (node.attributes.hasNode == 0 && jsonPost.postList != "") {
                              
                             $(‘#mytree‘).tree(‘append‘, {
                                 parent: node.target,
                                 data: jsonPost.postList
                             });
                             node.attributes.hasNode = 1;
                             //因为附加上之后无法自动刷新,所以先合并再
                             $("#mytree").tree(‘collapse‘, node.target);
                             $("#mytree").tree(‘expand‘, node.target);
                         }
                          
                          
                          
                     },
                     "json"
                 );
 
          }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/// <summary>
 /// 获取组织结构树(感觉有点慢,后期改成异步加载)
 /// </summary>
 /// <param name="isPost">是否需要岗位分类信息加载</param>
 /// <returns></returns>
 public string GetOrganizationTree(int isPost)
 {
     StringBuilder result = new StringBuilder();
     result.Append("[{\"id\":\"0\",\"text\":\"组织结构\",\"attributes\":{\"isKind\":\"-1\",\"level\":\"1\"},\"children\": ");
 
     OrganizationServiceRef.ORGANIZATION_KIND[] orgKind = orgClient.GetOrgKindModelList();
 
     result.Append("[");//肯定有分类这一层
     foreach (var kindItem in orgKind)
     {
         result.Append("{\"id\":\"" + kindItem.PK_ORG_KIND_ID + "\",\"text\":\"" + kindItem.ORG_TYPE_NAME + "\",\"attributes\":{\"isKind\":\"1\",\"level\":\"2\"}");//,\"state\":\"closed\",\"attributes\":[{\"isKind\":\"1\"}]
         OrganizationServiceRef.ORGANIZATION[] org = orgClient.GetOrganizationModelList(kindItem.PK_ORG_KIND_ID, "");
             if (org.Length > 0)
             {
                 result.Append(",\"children\":[");
                 foreach (var orgItem in org)
                 {
                     result.Append("{\"id\":\"" + orgItem.PK_ORG_ID + "\",\"text\":\"" + orgItem.ORG_FULL_NAME + "\"," +
                         "\"attributes\":{\"isKind\":\"0\",\"orgShortName\":\"" + orgItem.ORG_NAME + "\",\"orgCode\":\"" + orgItem.ORG_CODE + "\",\"orgIcon\":\"../../" + orgItem.ORG_ICON + "\",\"level\":\"3\"}");
                     if (isPost==1)//如果有岗位分类信息那么添加子节点
                     {
                         result.Append(",\"children\":[{\"id\":\"" + orgItem.PK_ORG_ID + "_1\",\"text\":\"岗位类型1\",\"state\": \"closed\",\"attributes\":{\"hasNode\":\"0\",\"isEm\":\"1\",\"level\":\"4\"}},{\"id\":\"" + orgItem.PK_ORG_ID + "_2\",\"text\":\"岗位类型2\",\"state\": \"closed\",\"attributes\":{\"hasNode\":\"0\",\"isEm\":\"0\",\"level\":\"4\"}}]");
                     
                     }
                      result.Append( " },");
                 }
                 result.Remove(result.Length-1,1);
                 result.Append("]");
             }
             else
             {
                 result.Append("");
             }
 
          result.Append("},");
 
     }
 
     result.Remove(result.Length - 1, 1);
     result.Append("]");//第一层的children end
 
      
 
     result.Append(" }]");
     return result.ToString();
 
 }

 因为我现在做的这个是组织机构和岗位放在两个表中,以上只是生成了组织的结构,加上岗位类型。

 

再展开岗位类型的时候用到了异步加载,后台代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public string GetGeneralPostForTree(string parentId, string isFirst, string isEm)
       {
            
             RoleServiceRef.ROLE[] postList=roleClient.GetGeneralPostList(parentId,isFirst,isEm);
             StringBuilder sbJsonPost = new StringBuilder();
             sbJsonPost.Append("{\"postList\":[ ");
             if (postList.Length > 0)
             {
 
 
                 foreach (var post in postList)
                 {
                     if (!(bool)post.ROLE_IS_POST)//如果不是岗位,那么在下边添加一个默认的节点,这个添加的节点在展开时如果有数据则删除掉,没有则存在加标示。
                     {
                         sbJsonPost.Append("{\"id\":\"" + post.PK_ROLE_ID + "\",\"text\":\"" + post.ROLE_NAME + "\",\"state\":\"closed\"}");
 
                     }
                     else
                     {
                         sbJsonPost.Append("{\"id\":\"" + post.PK_ROLE_ID + "\",\"text\":\"" + post.ROLE_NAME + "\"}");
 
                     }
 
                     sbJsonPost.Append(",");
                 }
                 sbJsonPost.Remove(sbJsonPost.Length-1, 1);
             }
             sbJsonPost.Append("]}");
 
 
             return sbJsonPost.ToString();
 
       }

jquery easyui tree 异步加载数据,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。