Advertisement
Guest User

jQuery UI Accordion Pagination

a guest
Apr 27th, 2011
2,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This is a plugin for the jQuery UI Accordion which
  3.  * makes it easy to build a pagination for it
  4.  * Version: 0.1
  5.  * Author: Michael Henke
  6.  */
  7.  
  8. /* Example Code */
  9. /*
  10. <script type="text/javascript">
  11.  
  12. var paginatorHandle = null;
  13.  
  14. jQuery(document).ready(function() {
  15.     jQuery("#dalist").accordion({
  16.         autoHeight: false
  17.     });
  18.  
  19.     paginatorHandle = jQuery("#dalist").paginateAccordion({
  20.             "currentPage": 0,
  21.             "itemsPerPage": 3,
  22.             "paginatorControl": jQuery("#accordionPaginator")
  23.         });
  24.    
  25.     // initial paginate call
  26.     paginatorHandle.paginate();
  27.    
  28.     jQuery("#accordionPaginator .nextPage").click(function() {
  29.         paginatorHandle.nextPage();
  30.     });
  31.    
  32.     jQuery("#accordionPaginator .previousPage").click(function() {
  33.         paginatorHandle.previousPage();
  34.     });
  35.    
  36.     jQuery("#accordionPaginator .goToPage").change(function() {
  37.         var pageIndex = parseInt($(this).val());
  38.         paginatorHandle.goToPage(pageIndex);
  39.     });
  40. });
  41. </script>
  42.  
  43. <div id="dalist">
  44.     <h3><a href="#">Section 1</a></h3>
  45.     <div>
  46.         <p>Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.</p>
  47.     </div>
  48.     <h3><a href="#">Section 2</a></h3>
  49.     <div>
  50.         <p>Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna. </p>
  51.     </div>
  52.     <h3><a href="#">Section 3</a></h3>
  53.     <div>
  54.         <p>Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui. </p>
  55.         <ul>
  56.             <li>List item one</li>
  57.             <li>List item two</li>
  58.             <li>List item three</li>
  59.         </ul>
  60.     </div>
  61.     <h3><a href="#">Section 4</a></h3>
  62.     <div>
  63.         <p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p><p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p>
  64.     </div>
  65.     <h3><a href="#">Section 5</a></h3>
  66.     <div>
  67.         <p>Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p><p>Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p>
  68.     </div>
  69. </div>
  70. <div id="accordionPaginator">
  71.     <a href="#" class="previousPage">Previous Page</a>
  72.     <select name="goToPage" class="goToPage" size="1"></select>
  73.     <a href="#" class="nextPage">Next Page</a>
  74. </div>
  75. */
  76.  
  77. // this is the main class
  78. function AccordionPaginator(element, currentPage, itemsPerPage, paginatorControl) {
  79.     this.element = element;
  80.     this.currentPage = currentPage;
  81.     this.itemsPerPage = itemsPerPage;
  82.     this.paginatorControl = paginatorControl;
  83.    
  84.     // does the actual pagination (shows/hides items)
  85.     this.paginate = function() {
  86.         var index = this.currentPage * this.itemsPerPage;
  87.        
  88.         element.accordion("activate", index);
  89.         element.children().hide();
  90.  
  91.         if(index < 0) {
  92.             this.element.children("div:first").show();
  93.             this.element.children("h3:first").show();  
  94.         }
  95.         else {
  96.        
  97.             this.element
  98.                 .children("div:eq(" + index + ")")
  99.                 .show();
  100.                
  101.             this.element
  102.                 .children("h3:eq(" + index + "),h3:gt(" + index + ")")
  103.                 .filter(":lt(" + this.itemsPerPage + ")")
  104.                 .show();
  105.         }
  106.        
  107.         this.refreshControl();
  108.     };
  109.    
  110.     // increments the currentPage var (if possible) and calls paginate
  111.     this.nextPage = function() {
  112.         if(this.currentPage + 1 >= this.getMaxPageIndex()) {
  113.             return;
  114.         }
  115.        
  116.         this.currentPage++;
  117.         this.paginate();
  118.     };
  119.    
  120.     // decrements the currentPage var (if possible) and calls paginate
  121.     this.previousPage = function() {
  122.         if(this.currentPage - 1 < 0) {
  123.             return;
  124.         }
  125.  
  126.         this.currentPage--;
  127.         this.paginate();
  128.     };
  129.    
  130.     // sets currentPage var (if possible) and calls paginate
  131.     this.goToPage = function(pageIndex) {
  132.         if((pageIndex < 0) || (pageIndex  >= this.getMaxPageIndex())) {
  133.             return;
  134.         }
  135.        
  136.         this.currentPage = pageIndex;
  137.         this.paginate();
  138.     };
  139.    
  140.     // returns the maximum of pages possible with the current number of items
  141.     this.getMaxPageIndex = function() {
  142.         var items = this.element.children("h3");
  143.         var fullPages = items.length / this.itemsPerPage;
  144.         var restPage = items.length % (this.itemsPerPage > 0 ? 1 : 0);
  145.         return fullPages + restPage;
  146.     };
  147.    
  148.     // fills up the paginator control
  149.     this.refreshControl = function() {
  150.         if(this.paginatorControl == null) {
  151.             return;
  152.         }
  153.        
  154.         var pageList = this.paginatorControl.children(".goToPage");
  155.         pageList.empty();
  156.         for(var i = 0; i < this.getMaxPageIndex(); i++) {
  157.             pageList.append("<option value=\"" + i + "\">" + (i + 1) + "</option>");
  158.         }
  159.         pageList.val(this.currentPage);
  160.     };
  161. }
  162.  
  163. jQuery.fn.extend({
  164.     paginateAccordion: function(options) {
  165.         var currentPage = options.currentPage ? parseInt(options.currentPage) : 0;
  166.         var itemsPerPage = options.itemsPerPage ? parseInt(options.itemsPerPage) : 5;
  167.         var paginatorControl = options.paginatorControl;
  168.        
  169.         return new AccordionPaginator(this, currentPage, itemsPerPage, paginatorControl);
  170.     }
  171. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement