htmlcsshtml-tabledynamic-tablestableheader

Static/sticky Header Using Dynamic Table


Please can someone guide me on how to implement a static (sticky) header to this dynamically created table?

I have tried multiple things from Stackoverflow threads for a while now but lack HTML/CSS knowledge and I'm obviously missing something simple.

I have managed to get it working using a table created directly in the main body of the code, but when I use my dynamically created tables from JSON I can't get anything to 'stick'.

Below the code:

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=0.50, maximum-scale=1, user-scalable=0"/>
<head>

    <title>iNews HTML Running Order</title>
    <style>
        table 
        {
            border: solid 1px #CCCCCC;
            border-collapse: collapse;
            text-align: left;
            font:30px Arial;
        }
        tr, th, td
        {
            white-space: nowrap;
            padding-right: 50px;
        }
        tr
        {
            background-color: #ffffff;
            border: solid 1px #CCCCCC;
        }
        th
        {
            background-color: #CCCCCC;
        }
        #container
        {
            text-align: center;
            max-width: 100%;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body onload="initialisePage('LW')">

    <p id="showData">Loading Running Order...</p>


</body>


<script>
    var loop;
    var filename;
    var table;

    function updateJSONData(filename)
    {
        getDataFromJSON(filename)
        loop = setInterval(function(){getDataFromJSON(filename);}, 500);
    }

    function initialisePage(newFilename)
    {
        filename = newFilename;
        updateJSONData(filename)
    }

    function setFileName(newFilename)
    {
        clearInterval(loop)
        filename = newFilename;
        updateJSONData(filename)
    }

    function getDataFromJSON(filename)
    {
        $.get( "http://10.142.32.72/dashboard/"+filename+".json", function( data ) {
            var myBooks = JSON.parse(data);
            CreateTableFromJSON(myBooks)
        });
    }

    function CreateTableFromJSON(myBooks)
    {
        var title = ["Page", "Slug", "Pres 1", "Pres 2", "CAM", "Format", "Clip Dur", "Total", "Backtime"];
        var col = ["page-number", "title", "pres1", "pres2", "camera", "format", "runs-time", "total-time", "back-time"];

        // CREATE DYNAMIC TABLE.
        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 = title[i];
            tr.appendChild(th);
        }

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

            tr = table.insertRow(-1);

            if (myBooks[i]["floated"] == "true"){
                tr.style.color = "#ffffff";
                tr.style.background = "blue";
            }

            if ((myBooks[i]["break"] == "true") && (myBooks[i]["floated"] == "false")){
                tr.style.background = "#00ff00";
            }

            for (var j = 0; j < col.length; j++) {
                var tabCell = tr.insertCell(-1);
                tabCell.innerHTML = myBooks[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);
        console.log("Refreshed: " + filename);
    }
</script>

</html>

Many thanks in advance,

Joe


Solution