socket.io 将对象发送到 html table
socket.io send an object to a html table
我是一个真正的新手 - 我得到了帮助来创造一些东西并且它正在使用 socket.io。我正在尝试 fiddle 绕过边缘,边走边学奇怪的东西。但是在我项目的早期阶段就碰壁了。它可能太深了......但只是为了防止在这里问我一个我能理解的答案 - 这里是:
我基本上有三个文件一起工作。 server.js、client.js 和 index.ejs(这是我的 html 文件)。我在“房间”中发生了一系列事情,现在我想在 html [=45= 中显示一个对象的值(接下来我想用它们做更多,但现在显示) ].
server.js
...它创建感兴趣的信息(使用当前有效的数据库调用)然后:
io.to(roomId).emit('room-location-update', result.rows);
client.js
...接收数据。我的 console.log 拥有一切。然后我分配一个变量来保存它以供在 html:
中使用
socket.on("room-location-update", (data_information) => {
console.log(data_information);
//I think I need code in here?
var wp = data_information;
}
index.ejs
...无法显示任何内容。我有一个 table 构造用于在一系列 table 单元格中使用来自 client.js 的变量,基本上所有构造为:
<td id=wp.name><td id=wp.radius>
但没有任何显示。首次创建 data_information 时,.name 和 .radius 属性在数据库中有效。
为什么没有显示? Google 搜索暗示(对我而言)这是基本的东西并且应该有效。很明显我缺少一些基本的东西(?)有什么想法吗?
抱歉,我实际上不知道你在谈论什么编辑。如果我做错了什么,请道歉 - 我是一个真正的新手。
最后我放弃了在 ejs 文件中这样做并创建了一个包含 html table 代码的大字符串并在 client.js.然后将其弹出到 ejs 文件中。不知道这是否有效,但它确实有效,万岁。
对我有用的截断代码片段(在 client.js 中):
var $table = "<table border='1'>"
$table += "<thead><tr><th>Player</th><th>Location</th></tr></thead><tbody>"
for (var i = 0; i < display_information.length; i++) {
$table += '<tr><td>' + display_information[i].id + '</td>'
$table += '<td>' + display_information[i].name + '</td>'
}
$table += "</tr></tbody></table>"
$('#displayinfo').empty().append($table);
然后在我的index.ejs中是: <pre><span id="displayinfo"></span></pre>
我是一个真正的新手 - 我得到了帮助来创造一些东西并且它正在使用 socket.io。我正在尝试 fiddle 绕过边缘,边走边学奇怪的东西。但是在我项目的早期阶段就碰壁了。它可能太深了......但只是为了防止在这里问我一个我能理解的答案 - 这里是:
我基本上有三个文件一起工作。 server.js、client.js 和 index.ejs(这是我的 html 文件)。我在“房间”中发生了一系列事情,现在我想在 html [=45= 中显示一个对象的值(接下来我想用它们做更多,但现在显示) ].
server.js ...它创建感兴趣的信息(使用当前有效的数据库调用)然后:
io.to(roomId).emit('room-location-update', result.rows);
client.js ...接收数据。我的 console.log 拥有一切。然后我分配一个变量来保存它以供在 html:
中使用socket.on("room-location-update", (data_information) => {
console.log(data_information);
//I think I need code in here?
var wp = data_information;
}
index.ejs ...无法显示任何内容。我有一个 table 构造用于在一系列 table 单元格中使用来自 client.js 的变量,基本上所有构造为:
<td id=wp.name><td id=wp.radius>
但没有任何显示。首次创建 data_information 时,.name 和 .radius 属性在数据库中有效。
为什么没有显示? Google 搜索暗示(对我而言)这是基本的东西并且应该有效。很明显我缺少一些基本的东西(?)有什么想法吗?
抱歉,我实际上不知道你在谈论什么编辑。如果我做错了什么,请道歉 - 我是一个真正的新手。
最后我放弃了在 ejs 文件中这样做并创建了一个包含 html table 代码的大字符串并在 client.js.然后将其弹出到 ejs 文件中。不知道这是否有效,但它确实有效,万岁。
对我有用的截断代码片段(在 client.js 中):
var $table = "<table border='1'>"
$table += "<thead><tr><th>Player</th><th>Location</th></tr></thead><tbody>"
for (var i = 0; i < display_information.length; i++) {
$table += '<tr><td>' + display_information[i].id + '</td>'
$table += '<td>' + display_information[i].name + '</td>'
}
$table += "</tr></tbody></table>"
$('#displayinfo').empty().append($table);
然后在我的index.ejs中是: <pre><span id="displayinfo"></span></pre>