简体   繁体   中英

Auto Pagination combine with Auto Refresh Div every X seconds

I'm setting up a datatables and i want to display it on monitor so that i need the datatables can refresh div and also auto pagination to the next page at the same time. When the div refresh it canceled the auto pagination and back to the first page. Please help me.

    <script type="text/javascript"> 
        $(document).ready(function() {
            var table = $('.data').DataTable({
                "searching": false,
                "info": false,
                "lengthChange": false
            });
            setInterval(function(){ 
            var info = table.page.info();
            var pageNum = (info.page < info.pages) ? info.page + 1 : 1;
            table.page(pageNum).draw(false);    
            }, 5000);  
        });

        $(document).ready(function() {
            setInterval(function () {
                $('#test').load(window.location.href + ' #test');
            }, 3000);
        });

    </script>

You can try reinitializing the Datatable like this

$(document).ready(function() {
  initDataTable();
  setInterval(function() {
    $('#test').load(window.location.href + ' #test', initDataTable);
  }, 3000);
});

function initDataTable() {  
  var table = $('.data').DataTable({
    "searching": false,
    "info": false,
    "lengthChange": false
  });
  setInterval(function() {
    var info = table.page.info();
    var pageNum = (info.page < info.pages) ? info.page + 1 : 1;
    table.page(pageNum).draw(false);
  }, 5000);
}

UPDATE

Use global variable for page number

var info;
var pageNum = 1;
var pageInterval;

$(document).ready(function() {
  initDataTable();
  setInterval(function() {
    $('#test').load(window.location.href + ' #test', initDataTable);
  }, 3000);
});

function initDataTable() {
  clearInterval(pageInterval)
  var table = $('.data').DataTable({
    "searching": false,
    "info": false,
    "lengthChange": false
  });
  pageInterval = setInterval(function() {
    info = table.page.info();
    pageNum = (pageNum + 1) % info.pages
    table.page(pageNum).draw(false);
  }, 5000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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