按下按钮后在Web浏览器中显示CSV

时间:2018-09-15 13:05:08

标签: javascript html d3.js css-tables import-from-csv

当我单击按钮时,我想以html显示CSV表(理想情况下,我希望以后能够选择其他CSV)。我正在使用d3.js,但到目前为止,即使导入数据,我也无法成功。有什么建议吗?

我正在使用的CSV是https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv

,并将其作为data.csv放在Default.htm旁边

// Library used: in local.
// https://d3js.org v5.7.0 Copyright 2018 Mike Bostock
// jQuery JavaScript Library v3.3.1


function myTable() {
    var data = d3.text('data.csv', function(error, _data){
        var table = d3.csv.parseRows(_data);
    });

}
.chart div{
    font: 10px sans-serif;
    background-color: steelblue;
    text-align: right;
    padding: 3px;
    margin: 1px;
    color: white;
}

.table div{
    font: 10px sans-serif;
    background-color: white;
    text-align: right;
    padding: 3px;
    margin: 1px;
    color: darkgreen;
}

body {
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    color: black;
    background-color: whitesmoke;
    margin: 0px;
    padding: 0;
}



p {
    line-height: 21px;
}

h1 {
    color: white;
    background-color: rgb(111, 0, 255);
    font-size: 2.5em;
    margin: 0;
    margin-top: 0px;
    padding: 5px;
    font-style: italic;
    text-align: left;
    border-top-style: solid;
    border-top-width: 0.5em;
    border-top-color: rgb(44, 0, 58);
}

table {
    border-collapse: collapse;
    border: 2px black solid;
    font: 12px sans-serif;
}

td {
    border: 1px black solid;
    padding: 5px;
}
<!DOCTYPE html>
<html>

<head>
    <title>Training web</title>
    <script src="JQuery\jquery-3.3.1.js"></script>
    <script src="d3\d3.js"></script>
    <script src="script.js"></script>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>Table Reader</h1>
    <p>This should show a table</p>
    <button onclick="myTable()">Click me</button>
    <div class="chart">
        
    </div>

    <div class="table">
        
    </div>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

  

我正在使用d3.js,但到目前为止,我什至无法成功   导入数据。

在d3 v5中,csv(url)函数返回一个promise,因此您需要像这样访问数据:

function loadTable(url) {
    var data = d3.csv(url)
        .then(drawTable)
        .catch(showError);
}

function drawTable(data) {
    // use the first row to get the column headers
    var keys = Object.keys(data[0]);
    
    // add the keys to the table header element
    d3.select("div.table thead")
        .selectAll("tr")
        .data([keys])
        .enter()
        .append("tr")
        .selectAll("th")
        .data(h => h)
        .enter()
        .append("th")
        .text(k => k || "model");
    
    // add the values to the table body elemeent
    d3.select("div.table tbody")
        .selectAll("tr")
        .data(data)
        .enter()
        .append("tr")
        .selectAll("td")
        .data(r => keys.map(k => r[k]))
        .enter()
        .append("td")
        .text(d => d);
}

function showError(text) {
    alert(text);
}
div.table {
    font: 10px sans-serif;
    background-color: white;
    text-align: right;
    padding: 3px;
    margin: 1px;
    color: darkgreen;
}

body {
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    color: black;
    background-color: whitesmoke;
    margin: 0px;
    padding: 0;
}

p {
    line-height: 21px;
}

h1 {
    color: white;
    background-color: rgb(111, 0, 255);
    font-size: 2.5em;
    margin: 0;
    margin-top: 0px;
    padding: 5px;
    font-style: italic;
    text-align: left;
    border-top-style: solid;
    border-top-width: 0.5em;
    border-top-color: rgb(44, 0, 58);
}

table {
    border-collapse: collapse;
    border: 2px black solid;
    font: 12px sans-serif;
}

th {
    text-align: center;
    color: black;
    padding: 5px;
}

td {
    border: 1px black solid;
    padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<html>

<head>
    <title>Training web</title>
</head>

<body>
    <h1>Table Reader</h1>
    <p>This should show a table</p>
    <button onclick="loadTable('https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv')">Click me</button>

    <div class="table">
      <table>
        <thead>
          <!-- header names rendered here -->
        </thead>
        <tbody>
          <!-- csv data rows rendered here -->
        </tbody>
    </div>
</body>

</html>

答案 1 :(得分:-1)

function myTable() {
    var data = d3.text('data.csv', function(error, _data){
        d3.select("p").text(_data.join('<br/>'));
    });
}