jQuery DataTables: "Load more" button - Client-side processing

Example for article at https://www.gyrocode.com/articles/jquery-datatables-load-more-button/

by gyrocode

HTML

<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js"></script>
<script src="https://gyrocode.github.io/jquery-datatables-pageLoadMore/1.0.2/js/dataTables.pageLoadMore.min.js"></script>
<h3><a href="https://www.gyrocode.com/articles/jquery-datatables-load-more-button/" target="_blank">jQuery DataTables: "Load more" button</a> <small>Client-side processing</small></h3>

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Extn.</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
</table>

<div class="dt-more-container">
   <button id="btn-example-load-more" style="display:none">Load More</button>
</div>

<div style="text-align:center">
<button id="btn-example-reload">Reload data</button>
<button id="btn-example-load-dataset-1">Load dataset 1</button>
<button id="btn-example-load-dataset-2">Load dataset 2</button>
</div>

<hr><a href="https://www.gyrocode.com/articles/tag/jquery-datatables/" target="_blank">See more articles about jQuery DataTables</a> on <a href="https://www.gyrocode.com/articles/" target="_blank">Gyrocode.com</a>

CSS

.dt-more-container {
   text-align:center;
   margin:2em 0;
}

JavaScript

$(document).ready(function (){
   var table = $('#example').DataTable({
      dom: 'frt',
      ajax: 'https://gyrocode.github.io/files/jquery-datatables/arrays.json',
      drawCallback: function(){
         // If there is some more data
         if($('#btn-example-load-more').is(':visible')){
            // Scroll to the "Load more" button
            $('html, body').animate({
               scrollTop: $('#btn-example-load-more').offset().top
            }, 1000);
         }

         // Show or hide "Load more" button based on whether there is more data available
         $('#btn-example-load-more').toggle(this.api().page.hasMore());
      }      
   });
 
   // Handle click on "Load more" button
   $('#btn-example-load-more').on('click', function(){  
      // Load more data
      table.page.loadMore();
   });

   // Handle click on "Reload data" button
   $('#btn-example-reload').on('click', function(){  
      // Reset "Load more" plug-in
      table.page.resetMore();
      
      // Load more data
      table.ajax.reload();
   });

   // Handle click on "Load dataset 1" button
   $('#btn-example-load-dataset-1').on('click', function(){  
      // Reset "Load more" plug-in
      table.page.resetMore();
   
      table.ajax.url('https://gyrocode.github.io/files/jquery-datatables/arrays.json').load();
   });
   
   // Handle click on "Load dataset 2" button
   $('#btn-example-load-dataset-2').on('click', function(){  
      // Reset "Load more" plug-in
      table.page.resetMore();
   
      table.ajax.url('https://gyrocode.github.io/files/jquery-datatables/arrays_100.json').load();
   });   
});