简体   繁体   English

Javascript分页 - 自动切换时间间隔

[英]Javascript pagination - Automatic switch time interval

I have a monitor showing some information in tables. 我有一个监视器在表格中显示一些信息。 I would like that when the table gets too big to fit on the screen, that the page would say "Page 1/2" and then switch automatically between the two pages, lets say every 10 second. 我希望当桌子太大而不适合屏幕时,页面会说“Page 1/2”,然后在两页之间自动切换,让我们说每10秒钟。

I have tried searching, and it seems there is alot of solutions for creating the pagination. 我试过搜索,似乎有很多解决方案来创建分页。 However, I have not been able to find a soloution describing the automatic switch. 但是,我还没能找到描述自动开关的解决方案。

If anyone has a good idea how to achieve this, I would very much appreciate it. 如果有人知道如何实现这一目标,我将非常感激。

Some sort of automatic scrolling could also be interesting. 某种自动滚动也可能很有趣。

Best 最好

You can try something like this: 你可以尝试这样的事情:

 var tableData = [{id:1,first_name:"Zechariah"}, {id:2,first_name:"Ivett"}, {id:3,first_name:"Tremayne"}, {id:4,first_name:"Thibaud"}, {id:5,first_name:"Patrica"}, {id:6,first_name:"Carmina"}, {id:7,first_name:"Feliks"}, {id:8,first_name:"Beryle"}, {id:9,first_name:"Blondy"}, {id:10,first_name:"Humbert"}, {id:11,first_name:"Maighdiln"}, {id:12,first_name:"Beret"}, {id:13,first_name:"Eugenio"}, {id:14,first_name:"Kellen"}, {id:15,first_name:"Urson"}, {id:16,first_name:"Huntley"}, {id:17,first_name:"Iolanthe"}, {id:18,first_name:"Bernardo"}, {id:19,first_name:"Park"}, {id:20,first_name:"Ame"}]; var maxItems = 5; var table = document.getElementsByTagName('tbody')[0]; var tableContainer = document.getElementById('table-container'); var activePage = 1; var totalPages = Math.ceil(tableData.length / maxItems); var myInterval; // insert left arrow var leftArrow = document.createElement('BUTTON'); var leftArrowContent = document.createTextNode('PREV'); leftArrow.appendChild(leftArrowContent); tableContainer.appendChild(leftArrow); leftArrow.addEventListener('click', function(){ // check if we got to the last page if (activePage === 1) { activePage = totalPages; } else { activePage--; } insertTable(); }); // insert right arrow var rightArrow = document.createElement('BUTTON'); var rightArrowContent = document.createTextNode('NEXT'); rightArrow.appendChild(rightArrowContent); tableContainer.appendChild(rightArrow); rightArrow.addEventListener('click', function(){ // check if we got to the last page if (activePage === totalPages) { activePage = 1; } else { activePage++; } insertTable(); clearInterval(myInterval); myInterval = setInterval(function(){ rightArrow.click(); }, 10000); }); // insert page counter var pageCounter = document.createElement('SPAN'); tableContainer.appendChild(pageCounter); // automate the switch interval myInterval = setInterval(function(){ rightArrow.click(); }, 10000); function insertTable() { var slicedData = tableData.slice((activePage - 1) * maxItems, activePage * maxItems); // empty the previous table table.innerHTML = ''; // insert tr/td's for (var index = 0; index < slicedData.length; index++) { var tableTr = document.createElement('TR'); table.appendChild(tableTr); for (var key in slicedData[index]) { var tableTd = document.createElement('TD'); var tdContent = document.createTextNode(slicedData[index][key]); tableTd.appendChild(tdContent) tableTr.appendChild(tableTd); } } // update page counter content var pageCounterContent = document.createTextNode('Page ' + activePage + '/' + totalPages); pageCounter.innerHTML = ''; pageCounter.appendChild(pageCounterContent); } insertTable(tableData); 
 td { border: 1px solid grey; padding: 3px; } 
 <div id="table-container"> <table> <tbody> </tbody> </table> </div> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM