<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.js"></script>
<meta charset="utf-8" http-equiv= "Content-Type" content= "text/html;charset=gbk" >
<HEAD>
<TITLE>滚动表格</TITLE>
<!--css样式是锁定表头不动的-->
<style type="text/css">
.table{
width: 100%;
left:30%;
border-collapse:collapse;
border-spacing:0;
}
.fixedThead{
display: block;
width: 100%;
}
.scrollTbody{
display: block;
height: 316px;
overflow: hidden;
width: 100%;
}
.table td {
width: 300px;
border-bottom: #333 1px dashed;
padding: 5px;
background-color:#ddd;
}
.table th {
width: 300px;
border-bottom: #333 1px dashed;
border-top: #333 1px dashed;
padding: 5px;
line-height:24px;
background-color:#cfc;
}
.table tr{
border-bottom: #333 1px dashed;
line-height:24px;
padding: 5px;
}
thead.fixedThead tr th:last-child {
color:#FF0000;
}
</style>
<SCRIPT >
//先在table的最后增加一行,然后再把第一行中的数据填充到新增加的行中,最后再删除table的第一行
function change(table){
var row = table.insertRow(table.rows.length);//在table的最后增加一行,table.rows.length是表格的总行数
for(j=0;j<table.rows[0].cells.length;j++){//循环第一行的所有单元格的数据,让其加到最后新加的一行数据中(注意下标是从0开始的)
var cell = row.insertCell(j);//给新插入的行中添加单元格
cell.height = "24px";//一个单元格的高度,跟css样式中的line-height高度一样
cell.innerHTML = table.rows[0].cells[j].innerHTML;//设置新单元格的内容,这个根据需要,自己设置
}
table.deleteRow(0);//删除table的第一行
};
function tableInterval(){
var table = document.getElementById("test");//获得表格
change(table);//执行表格change函数,删除第一行,最后增加一行,类似行滚动
};
setInterval("tableInterval()",2000);//每隔2秒执行一次change函数,相当于table在向上滚动一样
</SCRIPT>
</HEAD>
<body>
<div style="text-align: center;">
<table border="1" id="tb">
<tr>
<td>租户</td>
<td>水费</td>
<td>电费</td>
<td>卫生费</td>
<td>水表位置</td>
<td>电表位置</td>
<td>联系人</td>
<td>电话</td>
<td>房费</td>
<td>租赁合同期限</td>
</tr>
</table>
</div>
<script>
window.onload = function () {
var datanav = [{ "name": "张三", "position": "1", "powerrate": "2", "waterrate": "3", "healthcosts": "4", "watermeter": "5", "ammeter": "6", "linkman": "7", "phone": "8", "rental": "9", "tenancy": "10", "remark": "11" },
{ "name": "李四", "position": "11", "powerrate": "10", "waterrate": "9", "healthcosts": "8", "watermeter": "7", "ammeter": "6", "linkman": "5", "phone": "4", "rental": "3", "tenancy": "2", "remark": "1"},
{ "name": "老王", "position": "11", "powerrate": "10", "waterrate": "9", "healthcosts": "8", "watermeter": "7", "ammeter": "6", "linkman": "5", "phone": "4", "rental": "3", "tenancy": "2", "remark": "1"}];
var tb = document.getElementById("tb");
//添加最后一行合计
var netPayrollSUM = 0;
var salarySUM = 0;
var unpaidWagesSUM = 0;
for (var i = 0; i < datanav.length; i++) {
var row = tb.insertRow(tb.rows.length);
row.style.height = 60;
var c1 = row.insertCell(0);
c1.innerHTML = datanav[i].name;
var c2 = row.insertCell(1);
c2.innerHTML = datanav[i].waterrate;
var c3 = row.insertCell(2);
c3.style.color = 'rgb(42,152,41)';
c3.innerHTML = datanav[i].powerrate;
var c4 = row.insertCell(3);
c4.style.color = 'red';
c4.innerHTML = datanav[i].healthcosts;
var c5 = row.insertCell(4);
c5.innerHTML = datanav[i].watermeter;
var c6 = row.insertCell(5);
c6.innerHTML = datanav[i].ammeter;
var c7 = row.insertCell(6);
c7.innerHTML = datanav[i].linkman;
var c8 = row.insertCell(7);
c8.innerHTML = datanav[i].phone;
var c9 = row.insertCell(8);
c9.innerHTML = datanav[i].rental;
var c10 = row.insertCell(9);
c10.innerHTML = datanav[i].tenancy;
//合计总数
netPayrollSUM += parseFloat(datanav[i].waterrate);
salarySUM += parseFloat(datanav[i].powerrate);
unpaidWagesSUM += parseFloat(datanav[i].healthcosts);
}
var html = "<tr ><td bgcolor='rgb(208,207,121)' style = 'color:white'>合计</td><td>" + netPayrollSUM +
"</td><td>" + salarySUM + "</td><td style = 'color:red'>" + unpaidWagesSUM +
"</td></tr>"
$('#tb').append(html);
}
</script>
</body>
</HTML>