zoukankan      html  css  js  c++  java
  • json无限树----几个月前写的插件

    第一次写插件,实现json文件读取生成树,可折叠

    json 格式

    [
     {
      "id":1,
      "name":"一级菜单1",
      "open":false,
      "child":[
       {
        "id":2,
        "name":"二级菜单1",
        "open":false,
        "child":
        [
         {
          "id":3,
          "name":"三级菜单1"
         }
        ]
       },
       {
        "id":4,
        "name":"二级菜单2",
        "open":false,
        "child":
        [
         {
          "id":4.1,
          "name":"三级菜单1"
         },
         {
          "id":4.2,
          "name":"三级菜单2",
          "open":false,
          "child":[
           {
            "id":4.3,
            "name":"四级菜单1",
            "open":false,
            "child":[
             {
              "id":4.4,
              "name":"五级菜单1"
             },
             {
              "id":4.5,
              "name":"五级菜单2"
             },
             {
              "id":4.6,
              "name":"五级菜单3"
             },
             {
              "id":4.7,
              "name":"五级菜单4"
             },
             {
              "id":4.8,
              "name":"五级菜单5"
             },
             {
              "id":4.9,
              "name":"五级菜单6"
             },
             {
              "id":4.10,
              "name":"五级菜单7"
             }
            ]
           }
          ]
         }
        ]
       },
       {
        "id":5,
        "name":"二级菜单3"
       }
      ]
     },
     {
      "id":6,
      "name":"一级菜单2",
      "open":false,
      "child":[
       {
        "id":7,
        "name":"二级菜单1",
        "open":false,
        "child":
        [
         {
          "id":8,
          "name":"三级菜单1"
         }
        ]
       },
       {
        "id":9,
        "name":"二级菜单2"
       },
       {
        "id":10,
        "name":"二级菜单3"
       }
      ]
     }
    ]

    jsonTree.js

    (function($) {    
        $.fn.jsonTree=function(data){
            var SearchJSON=function(A,isopen){
              var L=A.length;
              var html="<ul style='display:block'>";
              if(isopen==false){
                html="<ul style='display:none'>";
              };
              for(var i=0;i<=L-1;i++){
                var flag=A[i].child;
                if(flag){
                  var str=SearchJSON(A[i].child,A[i].open);
                  html=html+"<li class='farList'><span data-status='down'><span class='incon1 clickThere'></span>"+A[i].name+str+"</span></li>"
                }
                else{
                  html=html+"<li class='sonList'><span>"+A[i].name+"</span></li>"
                }
              }
              html=html+"</ul>";
              return html;
            };
            var html=SearchJSON(data);
            $(this).append(html);
            $(this).on('click','.farList span.clickThere',function(evt){
                  var target=evt.target;
                  if(!target.matches(".farList span.clickThere")){ return };
                var flag=$(target).parent().attr('data-status');
                if(flag=="down"){
                  $(target).next().toggle();
                  $(target).removeClass("incon1").addClass('incon2').css({"display":"inline-block"});
                  evt.stopPropagation();
                  $(target).parent().attr('data-status','up');
                }
                else{
                  $(target).next().toggle();
                  $(target).removeClass("incon2").addClass('incon1').css({"display":"inline-block"});
                  evt.stopPropagation();
                  $(target).parent().attr('data-status','down');
                }
            });
        }
    })(jQuery);  

    demo引用

    <div id="tree" class="tree"></div>
    <script>
       $(function(){
            $.get("list.json").success(function(res){
                $("#tree").jsonTree(res);
            });
        });
    </script>            


    实现效果

  • 相关阅读:
    表的管理
    子查询
    sql语句
    基本sql语句与oracle函数
    Visual C# 2008+SQL Server 2005 数据库与网络开发6.1.1 报表服务概述
    Visual C# 2008+SQL Server 2005 数据库与网络开发 5.4 小结
    Visual C# 2008+SQL Server 2005 数据库与网络开发5.3.1 日期时间函数
    Visual C# 2008+SQL Server 2005 数据库与网络开发 5.3 函数
    Visual C# 2008+SQL Server 2005 数据库与网络开发第6章 数据报表
    Visual C# 2008+SQL Server 2005 数据库与网络开发5.2.2 GROUP BY
  • 原文地址:https://www.cnblogs.com/xiaoluoli/p/6553273.html
Copyright © 2011-2022 走看看