簡體   English   中英

使用節點 js/javascript 在 HTML 表中顯示來自 SQL 數據庫的數據

[英]Display data from a SQL database in an HTML table using node js/javascript

所以我創建了一個 HTML 文件,它有一個包含幾列的表格。 我需要做的是找到一種方法將我的 SQL 數據庫中的數據顯示到這個 HTML 表中。 我已經使用 Node 創建了與數據庫的連接,但我不知道如何在表中顯示數據。 我到處找,但我只能找到 PHP 代碼,我不想使用它。 我需要它在 Node.js 中。 我該如何開始? 謝謝你們。 到目前為止,這是我對 HTML 的了解:

    <div id="table">
        <table align="right" cellspacing="0" cellpadding="5">
            <tr>
                <th>Match </th>
                <th>Time</th>
                <th>Red 1</th>
                <th>S</th>
                <th>Red 2</th>
                <th>S</th>
                <th>Red 3</th>
                <th>S</th>
                <th>Blu 1</th>
                <th>S</th>
                <th>Blu 2</th>
                <th>S</th>
                <th>Blu 3</th>
                <th>S</th>
                <th>Red Sc</th>
                <th>Blu Sc</th>
            </tr>
        </table>
    </div>

關於 Javascript 文件,我已經創建了一個與 Azure 數據庫的連接和一個用於從中檢索數據的 SELECT 查詢。 現在,我只需要將我擁有的數據放在上面的 HTML 表格中。

舉個例子,你就明白了。

節點:

var express = require('express')
var app = express()

app.get('/getData', function(req, res) {
    ... // Get data from database
})

html:

<table>
  <thead>
    <th>id</th>
    <th>name</th>
  </thead>
  <tbody id="place-here"></tbody>
</table>
<script>
  $.get('/getData', {}, function(result) {
    let res = '';
    result.forEach(data => {
      res += `<tr><td>${data.id}</td><td>${data.something}</td></tr>`
    })
    $('#place-here').html(res)
  });
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM