简体   繁体   中英

json2html - Create HTML table

I was trying to find good JS Plugin that will help me to generate HTML <table /> structure and fill it with JSON data.

I found json2html , but unfortunately I don't see any useful examples or documentation on plugin's official website, nor any 3rd party websites that I can look into.

I created a function() that will generate some random data for me, that I will be using in this demo sample. Stored JSON template within variable that I will be using for json2html and created one <div /> with an ID that will be used for parsing JSON generated data using this plugin.

// Demo function to generate random content
// ->
function generateChartData() {
 for (var i = 0; i < 5; i++) {
    var file_id = Math.floor(Math.random() * 90000) + 10000,
        file_name = 'Dummy File ' + Math.floor(Math.random() * 20) + 30 + '.zip',
        file_clicks = Math.round(Math.random() * 480) + 820,
        file_downloads = Math.round(Math.random() * 160) + 420,
        file_conversion = (file_clicks / file_downloads).toFixed(2),
        file_profit = Math.round(Math.random() * 120) + 310,
        file_cpa = (file_profit / file_downloads).toFixed(2),
        file_epc = (file_profit / file_clicks).toFixed(2);

    chartData.push({
      file_id: file_id,
      file_name: file_name,
      file_clicks: file_clicks,
      file_downloads: file_downloads,
      file_conversion: file_conversion,
      file_cpa: file_cpa,
      file_epc: file_epc,
      file_profit: file_profit
    });
  }

  // create variables but no values
  var total_clicks, total_downloads, total_conversion, total_profit, total_cpa, total_epc;
}
// <-

// Create html template for further json parsing
var template = {
  "tag": "table",
  "class": "table table-striped table-hover",
  "children": [
    {
      "tag": "thead",
      "id": "json-head",
      "children": [
        {
          "tag": "tr",
          "children": [
            {
              "tag": "th",
              "html": "ID"
            },
            {
              "tag": "th",
              "html": "File Name"
            },
            {
              "tag": "th",
              "html": "Clicks"
            },
            {
              "tag": "th",
              "html": "Downloads"
            },
            {
              "tag": "th",
              "html": "Conversion"
            },
            {
              "tag": "th",
              "html": "Average CPA"
            },
            {
              "tag": "th",
              "html": "EPC"
            },
            {
              "tag": "th",
              "html": "Profit"
            }
          ]
        }
      ]
    },
    {
      "tag": "tbody",
      "id": "json-body",
      "children": [
        {
          "tag": "tr",
          "children": [
            {
              "tag": "td",
              "html": "${file_id}"
            },
            {
              "tag": "td",
              "html": "${file_name}"
            },
            {
              "tag": "td",
              "html": "${file_clicks}"
            },
            {
              "tag": "td",
              "html": "${file_downloads}"
            },
            {
              "tag": "td",
              "html": "${file_conversion}"
            },
            {
              "tag": "td",
              "html": "${file_cpa}"
            },
            {
              "tag": "td",
              "html": "${file_epc}"
            },
            {
              "tag": "td",
              "html": "${file_profit}"
            }
          ]
        }
      ]
    },
    {
      "tag": "tfoot",
      "id": "json-foot",
      "children": [
        {
          "tag": "tr",
          "children": [
            {
              "tag": "td",
              "colspan": "2",
              "html": "Total / Average"
            },
            {
              "tag": "td",
              "html": "${total_clicks}"
            },
            {
              "tag": "td",
              "html": "${total_downloads}"
            },
            {
              "tag": "td",
              "html": "${total_conversion}"
            },
            {
              "tag": "td",
              "html": "${total_cpa}"
            },
            {
              "tag": "td",
              "html": "${total_epc}"
            },
            {
              "tag": "td",
              "html": "${total_profit}"
            }
          ]
        }
      ]
    }
  ]
};

// Empty array for json data
var chartData = [];

// Calling DEMO function to generate json data
generateChartData();

// Parse json data and generate html
$("#json-parse").json2html(chartData, template);

However, you will probably noticed that something is wrong with my JSON template or json2html call. It creates invalid table and generates table for each JSON data parsing. So unfortunately that is not something that I wanted.

My thought is to create following template and parse required information into <tbody /> & <tfoot /> but not to create them all over again.

<table class="table table-striped table-hover">
<thead id="json-head">
    <tr>
        <th>ID</th>
        <th>File Name</th>
        <th>Clicks</th>
        <th>Downloads</th>
        <th>Conversion</th>
        <th>Average CPA</th>
        <th>EPC</th>
        <th>Profit</th>
    </tr>
</thead>

<tbody id="json-body">
    <tr>
        <td>${file_id}</td>
        <td>${file_name}</td>
        <td>${file_clicks}</td>
        <td>${file_downloads}</td>
        <td>${file_conversion}</td>
        <td>${file_cpa}</td>
        <td>${file_epc}</td>
        <td>${file_profit}</td>
    </tr>
</tbody>

<tfoot id="json-foot">
    <tr>
        <td colspan="2">Total / Average</td>
        <td>${total_clicks}</td>
        <td>${total_downloads}</td>
        <td>${total_conversion}</td>
        <td>${total_cpa}</td>
        <td>${total_epc}</td>
        <td>${total_profit}</td>
    </tr>
</tfoot>

I can't provide JSFiddle unfortunately, because I can't find any CDN for it. But here is Screenshot that will show you what the undesired outcome looks like.

json2html - 糟糕的结果

I know it is my structure of JSON or the plugin call that I did not setup properly, but if you don't mind helping me out here. It's a real headache, but may help other users as well.

json2Html is generating your template inside a loop with counter that equals with your json object count. So the result which you see is not wrong. If you don't want to repeat header and footer, just use those as another template with an object array.
Update : Thanks @Volune for example .

// Create html template for further json parsing
        var headertemplate = {
            "tag": "thead",
              "id": "json-head",
              "children": [
                {
                  "tag": "tr",
                  "children": [
                    {
                      "tag": "th",
                      "html": "ID"
                    },
                    {
                      "tag": "th",
                      "html": "File Name"
                    },
                    {
                      "tag": "th",
                      "html": "Clicks"
                    },
                    {
                      "tag": "th",
                      "html": "Downloads"
                    },
                    {
                      "tag": "th",
                      "html": "Conversion"
                    },
                    {
                      "tag": "th",
                      "html": "Average CPA"
                    },
                    {
                      "tag": "th",
                      "html": "EPC"
                    },
                    {
                      "tag": "th",
                      "html": "Profit"
                    }
                  ]
                }
              ]
        }
        var footertemplate = { 
              "tag": "tfoot",
              "id": "json-foot",
              "children": [
                {
                  "tag": "tr",
                  "children": [
                    {
                      "tag": "td",
                      "colspan": "2",
                      "html": "Total / Average"
                    },
                    {
                      "tag": "td",
                      "html": "${total_clicks}"
                    },
                    {
                      "tag": "td",
                      "html": "${total_downloads}"
                    },
                    {
                      "tag": "td",
                      "html": "${total_conversion}"
                    },
                    {
                      "tag": "td",
                      "html": "${total_cpa}"
                    },
                    {
                      "tag": "td",
                      "html": "${total_epc}"
                    },
                    {
                      "tag": "td",
                      "html": "${total_profit}"
                    }
                  ]
                }
              ]
        }
        var template = {
          "tag": "table",
          "class": "table table-striped table-hover",
          "children": [ 
            {
              "tag": "tbody",
              "id": "json-body",
              "children": [
                {
                  "tag": "tr",
                  "children": [
                    {
                      "tag": "td",
                      "html": "${file_id}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_name}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_clicks}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_downloads}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_conversion}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_cpa}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_epc}"
                    },
                    {
                      "tag": "td",
                      "html": "${file_profit}"
                    }
                  ]
                }
              ]
            } 
          ]
        }; 

    // Empty array for json data
    var chartData = [];

    // Calling DEMO function to generate json data
    generateChartData(); 
    // Parse json data and generate html
    $("#json-parse").json2html(chartData[0], headertemplate);
    $("#json-parse").json2html(chartData, template);
    $("#json-parse").json2html(chartData[0], footertemplate); //use your total json object with one array inside, instead chartData[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM