2011-11-29 122 views
0
$(document).ready(function(){ 
     setInterval(function(){ 
     $.ajax({ url: "getLiveData.php", 
      success: function(result){ 
       $.each(result, function(i, result){ 
        var t = $("table#insideTable"); 
        t.append('<tr><td>' + result.carNo + '</td><td>' + 
          result.carSpeed + '</td></tr>'); 
        }); 
      }, 
      dataType: "json" 
      }); 
     }, 600000); 
}); 

你好,我试图用上面的代码来更新车速,每10分钟一次。从以后的新数据取代当前在jQuery中的数据

--data上午10:20 -----

+-------+-------+ 
|car |speed | 
+-------+-------+ 
|1  |170 kph| 
+-------+-------+ 
|2  |150 kph| 
+-------+-------+ 
|3  |190 kph| 
+-------+-------+ 

--data于10:30 -----

+-------+-------+ 
|car |speed | 
+-------+-------+ 
|1  |180 kph| 
+-------+-------+ 
|2  |155 kph| 
+-------+-------+ 
|3  |174 kph| 
+-------+-------+ 

但是,运行后代码,从两个时间点获得的结果都是一个接一个地显示的(见下文)。

+-------+-------+ 
|car |speed | 
+-------+-------+ 
|1  |170 kph| 
+-------+-------+ 
|2  |150 kph| 
+-------+-------+ 
|3  |190 kph| 
+-------+-------+ 
|1  |180 kph| 
+-------+-------+ 
|2  |155 kph| 
+-------+-------+ 
|3  |174 kph| 
+-------+-------+ 

我真正想要的是从以后的新数据中取代当前的数据。

+-------+-------+ 
|car |speed | 
+-------+-------+ 
|1  |180 kph| 
+-------+-------+ 
|2  |155 kph| 
+-------+-------+ 
|3  |174 kph| 
+-------+-------+ 

任何人都可以帮助我吗?

非常感谢!

+1

你已经完全省略了代码的重要组成部分 - 回调功能! – Alnitak

+0

你怎么把它添加到dom中? –

+0

谢谢@Annitak提醒,已添加回调函数。 – Acubi

回答

2

您想使用.html()功能代替.append()

变化:

success: function(result){ 
    $.each(result, function(i, result){ 
     var t = $("table#insideTable"); 
     t.append('<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>'); 
    }); 
} 

要:

success: function(result){ 

    //create an output variable that will be added to the DOM when we're finished 
    var output = ''; 

    //iterate through the results of the AJAX request 
    $.each(result, function(i, result){ 

     //add a row to the output variable 
     output += '<tr><td>' + result.carNo + '</td><td>' + result.carSpeed + '</td></tr>'; 
    }); 

    //add the output to the DOM, notice that since we are only adding nodes to the DOM once this creates less CPU overhead than appending each row separately (we also save overhead by not selecting the table element during each iteration of the $.each() loop) 
    $("#insideTable").html(output); 
} 

将取代HTML表元素中,而不是追加到它。下面是使用.html().append()的演示:http://jsfiddle.net/jasper/TKaVF/

这里是.html()的文档:http://api.jquery.com/html

在一个侧面说明,它不添加table,因为你是looking-让你选择任何更快增加一个ID(这个ID本身就相当快)。

var t = $("table#insideTable"); 

会更快:

var t = $("#insideTable"); 
+0

但目前他的追加是在每个语句中 - 每次你会覆盖整个表?授予他的问题格式在各地都很难阅读! (更新了现在问题中的格式....) – ManseUK

+0

我也注意到了,并且在将代码添加到DOM之前编辑我的代码以构建输出(这对性能来说更好)。 – Jasper

+0

+1 cool ...看起来不错...我在IE中遇到了几个问题,之前替换了

的innerHTML - 这就是为什么我在我的答案中重写了
元素.... – ManseUK

1

工作例如:http://jsfiddle.net/manseuk/mnRTf/

改变对你的成功方法:

$('#tablecontainerdiv').html('<table>'); 
$.each(result, function(i, result){ 
    $("#tablecontainerdiv table").append('<tr><td>' + result.carNo + '</td><td>' + 
       result.carSpeed + '</td></tr>'); 
}); 
$('#tablecontainerdiv').append('</table>'); 

其中tablecontainerdiv是表包含在例如一个div:

<div id="tablecontainerdiv"> 
    <table> 
    //your data here 
    </table> 
</div> 

新的代码将用新数据替换旧表格

0

只是空表,然后添加新行:

success: function(result){ 
      var t = $("table#insideTable"); // moved outside the loop 
      t.empty();      // remove contents of the element 
      $.each(result, function(i, result){ 
       t.append('<tr><td>' + result.carNo + '</td><td>' + 
         result.carSpeed + '</td></tr>'); 
       }); 
     } 
相关问题