Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is a plugin for the jQuery UI Accordion which
- * makes it easy to build a pagination for it
- * Version: 0.1
- * Author: Michael Henke
- */
- /* Example Code */
- /*
- <script type="text/javascript">
- var paginatorHandle = null;
- jQuery(document).ready(function() {
- jQuery("#dalist").accordion({
- autoHeight: false
- });
- paginatorHandle = jQuery("#dalist").paginateAccordion({
- "currentPage": 0,
- "itemsPerPage": 3,
- "paginatorControl": jQuery("#accordionPaginator")
- });
- // initial paginate call
- paginatorHandle.paginate();
- jQuery("#accordionPaginator .nextPage").click(function() {
- paginatorHandle.nextPage();
- });
- jQuery("#accordionPaginator .previousPage").click(function() {
- paginatorHandle.previousPage();
- });
- jQuery("#accordionPaginator .goToPage").change(function() {
- var pageIndex = parseInt($(this).val());
- paginatorHandle.goToPage(pageIndex);
- });
- });
- </script>
- <div id="dalist">
- <h3><a href="#">Section 1</a></h3>
- <div>
- <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>
- </div>
- <h3><a href="#">Section 2</a></h3>
- <div>
- <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>
- </div>
- <h3><a href="#">Section 3</a></h3>
- <div>
- <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>
- <ul>
- <li>List item one</li>
- <li>List item two</li>
- <li>List item three</li>
- </ul>
- </div>
- <h3><a href="#">Section 4</a></h3>
- <div>
- <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>
- </div>
- <h3><a href="#">Section 5</a></h3>
- <div>
- <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>
- </div>
- </div>
- <div id="accordionPaginator">
- <a href="#" class="previousPage">Previous Page</a>
- <select name="goToPage" class="goToPage" size="1"></select>
- <a href="#" class="nextPage">Next Page</a>
- </div>
- */
- // this is the main class
- function AccordionPaginator(element, currentPage, itemsPerPage, paginatorControl) {
- this.element = element;
- this.currentPage = currentPage;
- this.itemsPerPage = itemsPerPage;
- this.paginatorControl = paginatorControl;
- // does the actual pagination (shows/hides items)
- this.paginate = function() {
- var index = this.currentPage * this.itemsPerPage;
- element.accordion("activate", index);
- element.children().hide();
- if(index < 0) {
- this.element.children("div:first").show();
- this.element.children("h3:first").show();
- }
- else {
- this.element
- .children("div:eq(" + index + ")")
- .show();
- this.element
- .children("h3:eq(" + index + "),h3:gt(" + index + ")")
- .filter(":lt(" + this.itemsPerPage + ")")
- .show();
- }
- this.refreshControl();
- };
- // increments the currentPage var (if possible) and calls paginate
- this.nextPage = function() {
- if(this.currentPage + 1 >= this.getMaxPageIndex()) {
- return;
- }
- this.currentPage++;
- this.paginate();
- };
- // decrements the currentPage var (if possible) and calls paginate
- this.previousPage = function() {
- if(this.currentPage - 1 < 0) {
- return;
- }
- this.currentPage--;
- this.paginate();
- };
- // sets currentPage var (if possible) and calls paginate
- this.goToPage = function(pageIndex) {
- if((pageIndex < 0) || (pageIndex >= this.getMaxPageIndex())) {
- return;
- }
- this.currentPage = pageIndex;
- this.paginate();
- };
- // returns the maximum of pages possible with the current number of items
- this.getMaxPageIndex = function() {
- var items = this.element.children("h3");
- var fullPages = items.length / this.itemsPerPage;
- var restPage = items.length % (this.itemsPerPage > 0 ? 1 : 0);
- return fullPages + restPage;
- };
- // fills up the paginator control
- this.refreshControl = function() {
- if(this.paginatorControl == null) {
- return;
- }
- var pageList = this.paginatorControl.children(".goToPage");
- pageList.empty();
- for(var i = 0; i < this.getMaxPageIndex(); i++) {
- pageList.append("<option value=\"" + i + "\">" + (i + 1) + "</option>");
- }
- pageList.val(this.currentPage);
- };
- }
- jQuery.fn.extend({
- paginateAccordion: function(options) {
- var currentPage = options.currentPage ? parseInt(options.currentPage) : 0;
- var itemsPerPage = options.itemsPerPage ? parseInt(options.itemsPerPage) : 5;
- var paginatorControl = options.paginatorControl;
- return new AccordionPaginator(this, currentPage, itemsPerPage, paginatorControl);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement