3(D) : Read the data .csv file and draw Data Table

 

 Data Visualization 

( 3160717 )

Practical and Source Codes


<!DOCTYPE html>
<html lang="en-IN" dir="ltr">

<head>
    <title>Read CSV File to Data Table</title>
    <meta charset="utf-8">
    <meta name="author" content="SidPro" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <meta name="description" content="Data visualization using javaScript and HTML" />
    <script src="js/jquery-3.5.1.min.js"></script>
    <script src="js/jquery.csv.js"></script>
    <style>
        #mytable {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
        }

        #mytable td,
        #mytable th {
            border: 1px solid #ddd;
            padding: 8px;
        }

        #mytable tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        #mytable tr:hover {
            background-color: #ddd;
        }

        #mytable th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: cornflowerblue;
            color: white;
        }
    </style>
</head>

<body>
    <h1>Get data from CSV file</h1>
    <table id="mytable">
    </table>
</body>
<script>
    var content = "";
    $(document).ready(function () {
        //just change the name of .csv file to load file
        $.get('data/data.csv', function (theData) {
            console.log(theData);
            theData = theData.replace(/"/g, '');
            console.log(theData);

            theData = theData.split(/\r?\n|\r/);
            console.log(theData);
            totalRows = theData.length;

            theHead = theData[0].split(',');
            content += "<tr>";
            theHead.forEach(TH);
            content += "</tr>";


            for (let i = 1; i < totalRows; ++i) {
                theTD = theData[i].split(',');
                content += "<tr>";
                theTD.forEach(TD);
                content += "</tr>";
            }
            $('#mytable').html(content);


        });
    });

    function TH(value) {
        content += "<th>" + value + "</th>";
    }
    function TD(value) {
        content += "<td>" + value + "</td>";
    }
</script>

</html>




No comments:

Get new posts by email:


APY Logo