【问题标题】:Pass data to html table with socket io使用套接字 io 将数据传递给 html 表
【发布时间】:2019-11-24 14:10:21
【问题描述】:

我已经使用 API 中的数据,并且该数据将使用 socketio 实时显示在一个表中,其中它以对象的形式返回数据,例如:

"data": [
    {
      "name": "e7876319e4",
      "status_waiter": "Ready"
    },
    {
      "name": "e787631334",
      "status_waiter": "Ready"
    }
]

所以我想在 HTML 表格中显示该数据,如果数据发生更改,表格也必须更改,并且我在那里进行间隔设置,那么我该怎么做呢?

我尝试使用$('#status').append(data),这是成功的,但即使没有更改,它也会继续添加数据。

【问题讨论】:

  • 你试过$('#status').html(data)吗?

标签: javascript html websocket socket.io


【解决方案1】:

试一试:

$('#status').empty(); //removes existing
$('#status').append(data); //add new

作为:

function updateTable(){
    let data = []; // set data here
    $('#status').empty(); //removes existing
    $('#status').append(data); //add new

    setTimeout(()=>{updateTable();}, 1000); //call this after 1 second
}

// call this when ever you want to start the updatefor the first time, after that...
updateTable();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2019-09-04
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    相关资源
    最近更新 更多