Prerequisites
Content
- How it works (explanation)
- HTML
- JQuery
How does it work?
The Pagination or Paging I have implemented is not exactly the same most people might think it is.
Here using JQuery I am hiding all the data first and then display them as and when necessary.
So some key points that should be considered before using it:
- Users will have time to review each record individually and need not to scroll the page for all the records.
- This technique will not reduce the rendering time of the page because the jquery initializes once its the page is completely rendered.
E.g: If a DataGrid or Table contains has 100 records. The 100 records will first be rendered on the page. i.e it consumes the time required to render 100 records than the actual Page Size.
HTML
- <body>
- <div class="CSSTableGenerator" >
- <table ID="myTable">
- <tr>
- <td>Title 1</td><td>Title 2</td><td>Title 3</td>
- </tr>
- <tr><!-- Repeat The Rows no. of time -->
- <td>Row 1</td><td>Row 1</td><td>Row 1</td>
- </tr>
- </table>
- </div>
- <div align="center">
- <a href="#" id="prev"> < </a>
- <span id="PageIndex"></span>
- <a href="#" id="next"> > </a>
- </div>
- </body>
JQuery
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
- <script>
- var ps=5;
- var cp=0;
- var table;
- var tr;
- var totalPages=0;
- var anc ='<a href="javascript:SetPageIndex(NUMBER);">NUMBER</a>'
-
-
- var htmlpage ='';
- $(document).ready(function(){
- table = $('#myTable').get(0);
- tr = $('#myTable tr').get();
- totalPages = Math.ceil(tr.length/ps);
- for(var i=0;i<totalPages;i++)
- {
- htmlpage+=anc.replace('NUMBER',i).replace('NUMBER',i+1);
- }
- $('#PageIndex').html(htmlpage);
- run();
- Showbtn();
-
- $("#prev").click(function(e){
- cp--;
- run();
-
- Showbtn();
-
- e.preventDefault();
- });
- $("#next").click(function(e){
- cp++;
- run();
- Showbtn();
- e.preventDefault();
- });
- });
- var counter
- function SetPageIndex(counter)
- {
- cp=counter;
- run();
- Showbtn();
-
- }
-
- function run(){
- for(var i=0;i<tr.length;i++)
- {
- $(tr[i]).css("display","none");
- }
- var startIndex=ps*cp;
-
- var endIndex=startIndex+ps;
- if(startIndex==0)
- {
- for(var i=startIndex;i<endIndex+1;i++)
- {
- $(tr[i]).css("display","block");
- }
- }
- else
- {
- for(var i=startIndex;i<endIndex;i++)
- {
- $(tr[i]).css("display","block");
- }
- }
- $(tr[0]).css("display","block");
- $("#myTable thead").css("display","block");
- }
-
- function Showbtn()
- {
- $("#prev").show();
- $("#next").show();
- if(cp==0)
- {
- $("#prev").hide();
- $("#next").show();
- }
- if(cp==(totalPages-1))
- {
- $("#prev").show();
- $("#next").hide();
- }
- }
-
-
- </script>