1. 程式人生 > 其它 >jquery實戰(一)——jquery實現table的滾動

jquery實戰(一)——jquery實現table的滾動

技術標籤:JQueryjquerytable滾動

系列文章目錄

提示:這裡可以新增系列文章的所有文章的目錄,目錄需要自己手動新增
例如:第一章 Python 機器學習入門之pandas的使用


jquery實現table的滾動


目的

因為業務的需要,我們希望實現實現table底部資料滾動,如下圖
在這裡插入圖片描述


實現方法

1.HTML程式碼

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset
="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>資料視覺化大屏</title> <link rel="stylesheet" href="${ctxStatic}/visualdata/css/common.css"
>
<link rel="stylesheet" href="${ctxStatic}/visualdata/css/data_text.css"> </head> <body > <!--header開始--> <div class="header"> <div class="header_logo fl"> <a href="#"><img src="${ctxStatic}/visualdata/img/logo.png"
>
</a> </div> <div class="header_nav fl" align="center"> <div style="font-size:40px;">中鐵十一局</div> <div style="font-size:20px;">香山嶺隧道施工人員及車輛實時定位跟蹤系統</div> </div> <!-- <div class="header_myself fr"> <div class="topTime">時間載入中...</div> </div> --> </div> <!--header結束--> <!--content開始--> <div class="data_content"> <div class="data_info" align="center"> <div class="wendu" >溫度:9℃</div> <div class="shidu">溼度:56%RH</div> <div class="shidu">風速:3級</div> <div class="shidu">風向:東北風</div> <div class="shidu">PM2.5:21</div> </div> <div class="data_main"> <div class="main_center fl"> <div class="center_text"> <div class="main_title"> <img src="${ctxStatic}/visualdata/img/title_3.png" alt=""> 隧道平面模擬圖 </div> <iframe SRC="${ctx}/bbls/location/realTime" style="width:100%;height:100%"></iframe> </div> <!-- <iframe SRC="${ctx}/bbls/location/realTime" ></iframe> --> </div> </div> <div class="data_bottom"> <div class="bottom_1 fl"> <div class="main_title"> <img src="${ctxStatic}/visualdata/img/title_2.png" alt=""> 隧道工程車輛統計 </div> <div class="main_table"> <table class="carTable"> <thead> <tr> <th >車輛編號</th> <th>車輛型別</th> <th>駕駛人</th> <th>位置</th> <th>狀態</th> </tr> </thead> <tbody id="car"> </tbody> </table> </div> </div> <div class="bottom_4 fr"> <div class="main_title"> <img src="${ctxStatic}/visualdata/img/title_2.png" alt=""> 隧道作業人員統計 </div> <div class="main_table"> <table class="workerTable"> <thead> <tr> <th>姓名</th> <th>工種類別</th> <th>位置</th> <th>狀態</th> </tr> </thead> <tbody id="worker"> </tbody> </table> </div> </div> </div> </div> <!--content結束--> <script src="${ctxStatic}/visualdata/js/jquery-2.2.1.min.js"></script> </body> <script src="${ctxStatic}/visualdata/js/common.js"></script> </html>

2.JS程式碼

$(function () {
    getCurrentCar();
    getCurrentWorker();
})
setInterval(currrentWorker, 30000); 
function currrentWorker(){
	console.log(11);
	getCurrentCar();
    getCurrentWorker();
}
var carList;
var workerList;

	 //查詢車輛
	 var getCurrentCar = function(){
		   var URL = "${ctx}/../findCurrentCar"
		   $.ajax({
				type : "get",
				url : URL,
				dataType : "json",
				success : function(data) {
					if(data.list.length > 0){
						$("#car").html("");
						carList = data.list;
						for (var i = 0; i < carList.length; i++) {
							if(i<=4){
								var temp = "";
								if(carList[i].isSos==0){
									temp="正常";
								}else{
									temp="求救";
								}
								if(temp=="求救"){
									$("#car").append('<tr class="bb"><td>'+carList[i].carCode+'</td><td>'+carList[i].dictName+'</td><td>'+carList[i].people+'</td><td>'+carList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
								}else{
									$("#car").append('<tr><td>'+carList[i].carCode+'</td><td>'+carList[i].dictName+'</td><td>'+carList[i].people+'</td><td>'+carList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
								}
								
							}
						}
					}
				},
				error : function(data) {

				}
			});
	 }
	 
	 //查詢工人
	 var getCurrentWorker = function(){

		   var URL = "${ctx}/../findCurrentWorker"
		   $.ajax({
				type : "get",
				url : URL,
				dataType : "json",
				success : function(data) {
					var workerData = data;
					if(data.list.length > 0){
						 workerList = data.list;
						$("#worker").html("");
						for (var i = 0; i < workerList.length; i++) {
							if(i<=4){
								var temp = "";
								if(workerList[i].isSos==0){
									temp="正常";
								}else{
									temp="求救";
								}
								if(temp=="求救"){
									$("#worker").append('<tr class="bb"><td>'+workerList[i].name+'</td><td>'+workerList[i].dictName+'</td><td>'+workerList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
								}else{
									$("#worker").append('<tr><td>'+workerList[i].name+'</td><td>'+workerList[i].dictName+'</td><td>'+workerList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
								}
								
							}else{
								break;
							}
						}
					}
					
				},
				error : function(data) {

				}
			});
	 }

//表格滾動
	 function tableScroll(){
		 if(carList != undefined){
			 var car = carList[0];
			 if(carList.length >1){
					carList.splice(0,1);
					carList.push(car);
				}
			 $("#car").html("");
				for (var i = 0; i < carList.length; i++) {
					if(i<=4){
						var temp = "";
						if(carList[i].isSos==0){
							temp="正常";
						}else{
							temp="求救";
						}
						if(temp=="求救"){
							$("#car").append('<tr class="bb"><td>'+carList[i].carCode+'</td><td>'+carList[i].dictName+'</td><td>'+carList[i].people+'</td><td>'+carList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
						}else{
							$("#car").append('<tr><td>'+carList[i].carCode+'</td><td>'+carList[i].dictName+'</td><td>'+carList[i].people+'</td><td>'+carList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
						}
						
					}else{
						break;
					}
				}
	
		 }
		 
		if(workerList != undefined){
				var worker = workerList[0];
				if(workerList.length >1){
					workerList.splice(0,1);
					workerList.push(worker);
				}
				$("#worker").html("");
				for (var i = 0; i < workerList.length; i++) {
					if(i<=4){
						var temp = "";
						if(workerList[i].isSos==0){
							temp="正常";
						}else{
							temp="求救";
						}
						if(temp=="求救"){
							$("#worker").append('<tr class="bb"><td>'+workerList[i].name+'</td><td>'+workerList[i].dictName+'</td><td>'+workerList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
						}else{
							$("#worker").append('<tr><td>'+workerList[i].name+'</td><td>'+workerList[i].dictName+'</td><td>'+workerList[i].realLocation+'米'+'</td><td>'+temp+'</td></tr>');
						}
						
					}else{
						break;
					}
				}
				
				
		}
		 
	 }
	 
	 setInterval(tableScroll, 1000); 

3.總結

到此就實現了相關的目的。

總結

本文到此結束,希望廣大網友多多點評,指點。