Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

json data from server into html table

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    <style>
        table 
        {
            width: 100%;
            font: 17px Calibri;
        }
        table, th, td 
        {
            border: solid 1px #DDD;
            border-collapse: collapse;
            padding: 2px 3px;
            text-align: center;
        }
    </style>
</head>
<body>
    <p>Data extracted from a JSON and converted to an HTML table.</p>
    <div id="showData"></div>
</body>

<script>
    $(document).ready(function () {
        $.getJSON("../../library/sample.json", function (data) {

            var arrItems = [];      // THE ARRAY TO STORE JSON ITEMS.
            $.each(data, function (index, value) {
                arrItems.push(value);       // PUSH THE VALUES INSIDE THE ARRAY.
            });

            // EXTRACT VALUE FOR TABLE HEADER.
            var col = [];
            for (var i = 0; i < arrItems.length; i++) {
                for (var key in arrItems[i]) {
                    if (col.indexOf(key) === -1) {
                        col.push(key);
                    }
                }
            }

            // CREATE DYNAMIC TABLE.
            var table = document.createElement("table");

            // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

            var tr = table.insertRow(-1);                   // TABLE ROW.

            for (var i = 0; i < col.length; i++) {
                var th = document.createElement("th");      // TABLE HEADER.
                th.innerHTML = col[i];
                tr.appendChild(th);
            }

            // ADD JSON DATA TO THE TABLE AS ROWS.
            for (var i = 0; i < arrItems.length; i++) {

                tr = table.insertRow(-1);

                for (var j = 0; j < col.length; j++) {
                    var tabCell = tr.insertCell(-1);
                    tabCell.innerHTML = arrItems[i][col[j]];
                }
            }

            // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
            var divContainer = document.getElementById("showData");
            divContainer.innerHTML = "";
            divContainer.appendChild(table);
        });
    });
</script>
</html>
Comment

json data from server into html table

[
    {
        "Book ID": "1",
        "Book Name": "Computer Architecture",
        "Category": "Computers",
        "Price": "125.60"
    },
    {
        "Book ID": "2",
        "Book Name": "Asp.Net 4 Blue Book",
        "Category": "Programming",
        "Price": "56.00"
    },
    {
        "Book ID": "3",
        "Book Name": "Popular Science",
        "Category": "Science",
        "Price": "210.40"
    }
]
Comment

PREVIOUS NEXT
Code Example
Javascript :: handling props in functional components reactjs ijnterview questions 
Javascript :: add backgorund image in react native app 
Javascript :: Detect external link Click javascript 
Javascript :: 1541847516 
Javascript :: show selected text in textarea && activeElement 
Javascript :: how to extract java script elemet 
Javascript :: javascript code to jquery online 
Javascript :: native base change track color 
Javascript :: if no input given in javascript how to set default code 
Javascript :: rails json exclude nested attribute 
Javascript :: debounce="300" 
Javascript :: angular material nested tree 
Javascript :: 5.3.1.1. Logical AND¶ 
Javascript :: encryption decryption in javascript 
Javascript :: exchange array.include(string) in Javascript to array.indexOf(string) == -1 in Typescript 
Javascript :: moment format escape 
Javascript :: delete multiple nodes in firebase realtime database 
Javascript :: https://www.npmjs.com/package/number-in-letters 
Javascript :: localstorege remove 
Javascript :: how array sort works internally in javascript 
Javascript :: sorting json array by key in angular 9 
Javascript :: format large texts 
Javascript :: Unexpected eval or arguments in strict mode 
Javascript :: forintlol 
Javascript :: array object sort by date 
Javascript :: javascript call url without going to it 
Javascript :: reactjs copytoclipboard box 
Javascript :: one page nav cdn 
Javascript :: dynamic operator javascript if statement 
Javascript :: serve file nodejs ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =