MrMistreater

Paginate JSON data from controller using AJAX

Apr 24th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.     var pageSize = 10;
  5.     var currentPageIndex = 0;
  6.     var pageCount = 0;
  7.  
  8.     var startItemIndex = 0;
  9.     var itemsToDisplay = 0;
  10.  
  11.     function PaginateSetPageObjects() {
  12.         $('#pageTitle').text('Page ' + (currentPageIndex + 1) + ' of ' + pageCount);
  13.  
  14.         if (pageCount <= 1) {
  15.             $('#nextPage').hide();
  16.             $('#previousPage').hide();
  17.             $('#lastPage').hide();
  18.             $('#firstPage').hide();
  19.         } else {
  20.             $('#nextPage').show();
  21.             $('#previousPage').show();
  22.             $('#lastPage').show();
  23.             $('#firstPage').show();
  24.  
  25.             if (currentPageIndex == 0) {
  26.                 $('#previousPage').hide();
  27.                 $('#firstPage').hide();
  28.             } else if (currentPageIndex == (pageCount - 1)) {
  29.                 $('#nextPage').hide();
  30.                 $('#lastPage').hide();
  31.             }
  32.         }
  33.     }
  34.  
  35.     function PaginateCalculatePageIndexes(length) {
  36.         pageCount = 1;
  37.  
  38.         if (length > pageSize) {
  39.             if (length / pageSize > parseInt(length / pageSize)) {
  40.                 pageCount = parseInt(length / pageSize) + 1;
  41.             } else {
  42.                 pageCount = length / pageSize;
  43.             }
  44.         }
  45.         startItemIndex = currentPageIndex * pageSize;
  46.         var remainingItems = length - startItemIndex;
  47.         itemsToDisplay = pageSize + startItemIndex;
  48.  
  49.         if (remainingItems < pageSize) {
  50.             itemsToDisplay = remainingItems + startItemIndex;
  51.         }
  52.     }
  53.    
  54.     function displayImages() {
  55.         $.ajax({
  56.             type: "POST",
  57.             dataType: "json",
  58.             url: "/ControllerName/ActionName",
  59.             data: {},
  60.             success: function (jsonresults) {
  61.                 $("#imageContent").find("a").remove().end();
  62.                 PaginateCalculatePageIndexes(jsonresults.length);
  63.  
  64.                 if (jsonresults.length > 0) {
  65.                     for (var i = startItemIndex; i < itemsToDisplay; i++) {
  66.  
  67.                     }
  68.                 }
  69.                
  70.                 PaginateSetPageObjects();
  71.             },
  72.             error: function (cc, bb, aa) { alert(aa); }
  73.         });
  74.     }
  75.  
  76.     function moveFirstPage() { currentPageIndex = 0; displayObjects(); }
  77.     function moveLastPage() { currentPageIndex = pageCount - 1; displayObjects(); }
  78.     function movePreviousPage() { currentPageIndex--; displayObjects(); }
  79.     function moveNextPage() { currentPageIndex++; displayObjects(); }
  80.    
  81.     $(document).ready(function () { displayObjects(); });
  82. </script>
  83. </head>
  84. <body>
  85. <tableid="recordNav">
  86.     <tr>
  87.         <td nowrap="nowrap"><a href="javascript:void(0);" onclick="moveFirstPage()" id="firstPage"><img src="/Content/first.png" alt="" /></a></td>
  88.         <td nowrap="nowrap"><a href="javascript:void(0);" onclick="movePreviousPage()" id="previousPage"><img src="/Content/previous.png" alt="" /></a></td>        
  89.         <td id="pageTitle"></td>        
  90.         <td nowrap="nowrap"><a href="javascript:void(0);" onclick="moveNextPage()" id="nextPage"><img src="/Content/next.png" alt="" /></a></td>        
  91.         <td nowrap="nowrap"><a href="javascript:void(0);" onclick="moveLastPage()" id="lastPage"><img src="/Content/last.png" alt="" /></a></td>
  92.     </tr>
  93. </table>
  94. <table>
  95.     <thead>
  96.         <tr>
  97.             <th style="width:1%"></th>
  98.             <th style="width:94%" nowrap="nowrap">Description</th>
  99.             <th style="width:1%" nowrap="nowrap">Date</th>
  100.             <th style="width:1%" nowrap="nowrap">Acquisition Cost</th>
  101.             <th style="width:1%" nowrap="nowrap">Installation Cost</th>
  102.             <th class="last" style="width:1%" nowrap="nowrap">Delivery Cost</th>
  103.         </tr>
  104.     </thead>
  105.     <tbody id="acquisitionContent"></tbody>
  106. </table>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment