我正在使用NodeJS进行项目,我正在尝试使用AJAX读取我的MySQL数据库,然后在HTML表格中显示数据库表行。
我认为一旦将数据从数据库中删除,我就会遇到如何处理/存储数据的问题。
这是我的数据库的结构(当然是完全虚构的数据),到目前为止我的代码:
如果我是console.log(serverData),这里也是我得到的;在我看来,我的数据应该是某种嵌套数组或某种结构,允许我识别我正在访问的数组中的值。
server.js
<!DOCTYPE html>
<head>
<title>Home | Innovation One</title>
<link type="text/css" rel="stylesheet" href="css/styles.css" />
</head>
<body>
<h1>Dev sheet dashboard</h1>
<button id="getServers">Get servers</button>
<table id="servers">
<thead>
<tr>
<th width="150">Server name</th>
<th width="150">Client</th>
<th width="150">Type</th>
<th width="150">Host</th>
<th width="150">SSH</th>
<th width="150">MySQL</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="log"></div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Code here
$('button#getServers').on('click',function(){
var jqxhr = $.get('/get-servers/', function(data,fields) {
var serverData = JSON.parse(data);
console.log(serverData);
var rows = serverData.length / (fields.length - 1);
for (i=0 ; i < rows ; i++){
$('#servers tbody').append('<tr> <td>'+serverData[i]+'</td><td>'+serverData[i]+'</td> <td>'+serverData[i]+'</td><td>'+serverData[i]+'</td><td>'+serverData[i]+'</td><td>'+serverData[i]+'</td></tr>');
}
});
});
});
</script>
<script type="text/javascript" src="js/script.js" />
</body>
index.html
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.DisplayAlerts = False
If ActiveCell.Value = vbNullString Then
Exit Sub
Else
If Target.Comment Is Nothing And Target.CountLarge < 2 Then
Target.AddComment Now & " - " _
& "prev value = " _
& ActiveCell.Value & " - " _
& "new value: " _
& Target.Value & " - " _
& Environ("username") & " -" _
& "changed the value from a NULL value."
ElseIf Not Target.Comment Is Nothing Then
Target.Comment.Text _
vbNewLine & Now & " - " _
& "value changed to: " _
& Target.Value & " - by: " _
& Environ("username") & " -" _
, Len(Target.Comment.Text) + 1 _
, False
Exit Sub
End If
End If
If Target.CountLarge < 2 Then
Target.Comment.Shape.TextFrame.AutoSize = True
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub
答案 0 :(得分:1)
您的行数据实际上是对象数组:
[
{
name:'super_company',
client: 'Super Co Ltd',
type:'staging',
host:'156.34.567.34',
ssh:'gerbguiberug',
mysql:'49thgrekver'
},
{
},...
]
只需在数组上循环以获取单个对象,然后以简单的术语提取它的属性,或使用for in
循环或for each
循环。
示例:强>
for (i=0 ; i < rows.length ; i++){
var row = rows[i];
console.log(row.name);
console.log(row.client);
console.log(row.type);
console.log(row.host);
console.log(row.ssh);
console.log(row.mysql);
}
所以,你不需要把它推入一个数组来访问它。 更好的传递是直接的,访问如下:
<强> server.js:强>
connection.query('SELECT * FROM servers', function(err,rows,fields) {
if (err) {
console.log(err);
return
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(JSON.stringify(rows));
});
<强>的index.html:强>
$.get('/get-servers/', function(data,fields) {
var rows = JSON.parse(data);
for (i=0 ; i < rows.length ; i++){
var row = rows[i];
$('#servers tbody').append('<tr><td>'+row.name+'</td><td>'+row.client+'</td><td>'+row.type+'</td><td>'+row.host+'</td><td>'+row.ssh+'</td><td>'+row.mysql+'</td></tr>');
}
});
<强>加成强> 如果将内容类型设置为JSON会更好:
response.writeHead(200, {'Content-Type': 'application/json'});
这样你就不必在index.html(客户端)解析它。