• js解析多层json转为层级table,js动态添加td的倍数


    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            table td{
                 100px;
                height: 15px;
            }
        </style>
    </head>
    <body>
    <table id="mm" border="1" >
        <tbody id="tbody"></tbody>
    </table>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        var data = {
            "list": [{
                "list": [{
                    "list": "111", "value": "2"
                }], "value": "1"
            }, {
                "list": [{
                    "list": "222", "value": "3"
                }], "value": "4"
            }]
        }
    
        var trStr = "";
        $.each(data, function (tmp, tb) {
            $.each($(this), function (i, n) {
                trStr += '<tr height="25px" class="example">';//拼接处规范的表格形式
                trStr += '<td colspan="2" width="10%" align="center">' + n.value + '</td>';
                trStr += '<td colspan="2" width="10%" align="center"></td>';
                trStr += '</tr>';
                $.each(n.list, function (i1, n1) {
                    console.log(n1)
                    trStr += '<tr height="25px" class="example">';//拼接处规范的表格形式
                    trStr += '<td colspan="2" width="10%" align="center"></td>';
                    trStr += '<td colspan="2" width="10%" align="center">' + n1.value + '</td>';
                    trStr += '</tr>';
    
                })
            })
        })
    
    
        $("#tbody").html(trStr);
    </script>
    </body>
    </html>
    

    效果图如下:

  • 相关阅读:
    搜索框练习
    左侧菜单练习
    Maven Assembly插件介
    (总结)Nginx配置文件nginx.conf中文详解
    nginx、php-fpm、mysql用户权限解析
    全文检索引擎Solr的配置
    解决Discuz安装时报错“该函数需要 php.ini 中 allow_url_fopen 选项开启…”
    solr添加多个core
    精品站
    Win7下IIS的安装与配置
  • 原文地址:https://www.cnblogs.com/hanlei525/p/9613003.html
Copyright © 2020-2023  润新知