刷新表数据每5秒数据

2023-09-10 20:13:15 作者:-思密达っ

我在设计阶段的时刻,想知道我怎么会更新表每5秒。

我的表将显示来自我的模型返回的只读数据。

通常情况下我认为只会有<表>< /表> HTML,然后foreach循环写出来的数据

不过,因为我想刷新此整个表每隔5秒钟,我不能确定如何实现它。

我知道有JavaScript的setInterval函数,但我也不知该怎么办,在这一点上。难道是这样?

例如:/

 函数的getData()
{
    $ .getJSON(/ myController的/ myMethod的),

                 功能(数据){

                $每个(数据,功能(I,项目){
                    VAR行= {item.ID,item.Date,
                         item.Title
                    };
               $(#表).tableInsertRows(行);
                });

            });
}
    的setInterval(的getData,5000);
 

解决方案

这可能是最容易让你的 myMethod的的行动使返回JSON的视图来代替。然后,你可以只设置的innerHTML 一个div到Ajax响应。

否则,你的方法将工作,但你显然必须先删除现有的表行:

  $('#表)tableRemoveRows({来自:0,长度:???})。
 

修改

重读你的问题,这听起来像你问更多关于的setInterval 比实际创建表。你需要不断重新注册回调,所以是这样的:

 函数的getData()
{
    $ .getJSON(/ myController的/ myMethod的),功能(数据){

        $每个(数据,功能(I,项目){
                VAR行= {item.ID,item.Date,
                     item.Title
                };
           $(#表).tableInsertRows(行);
        });

        的setInterval(的getData,5000);

    });
}
的setInterval(的getData,5000);
 
ajax每隔5秒更新数据 精讲网页数据实时刷新实现

I am in the design stage at the moment and was wondering how I would update a table every 5 seconds.

My table will display read-only data returned from my model.

Normally my view would just have <table></table> HTML and then a foreach loop to write out the data.

However because I want to refresh this whole table every 5 seconds I am unsure how to implement it.

I know there is the javascript setinterval function but I'm also unsure what to do at that point. Would it be something like this?

eg/

function getdata()
{
    $.getJSON("/mycontroller/mymethod"), 

                 function(data) {

                $.each(data, function(i, item) {
                    var row = {  item.ID, item.Date,
                         item.Title
                    };
               $(#table).tableInsertRows(row);
                });

            });
}
    setInterval( "getdata", 5000 );

解决方案

It might be easiest to have your mymethod action render a view instead of returning JSON. Then you could just set the innerHTML of a div to the ajax response.

Otherwise your approach will work, but you obviously have to delete the existing table rows first:

$('#table').tableRemoveRows({from:0, length:???});

Edit

re-reading your question, it sounds like you're asking more about setInterval than actually creating the table. You need to keep re-registering the callback, so something like this:

function getdata()
{
    $.getJSON("/mycontroller/mymethod"), function(data) {

        $.each(data, function(i, item) {
                var row = {  item.ID, item.Date,
                     item.Title
                };
           $(#table).tableInsertRows(row);
        });

        setInterval( getdata, 5000 );

    });
}
setInterval( getdata, 5000 );