大量数据 简易jQuery分页插件 anypaginator

  • 源码大小:74.49KB
  • 所需积分:1积分
  • 源码编号:19JP-3087
  • 浏览次数:676次
  • 最后更新:2023年05月04日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

anypaginator是一个轻量级的jQuery分页插件,可以用来浏览大量数据,而不必加载整个页面。

这个插件设置简单,使用方便。它支持处理几乎所有的数据类型,如表、HTML列表,甚至远程JSON数据。

根据AGPL v3许可证发布。

如何使用它:

1.加载最新的jQuery库后,包括主脚本anyPaginator.js。

  1. <link href="/path/to/anyPaginator.css" rel="stylesheet" />
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <script src="/path/to/anyPaginator.js"></script>

2.使用anypaginator插件来处理静态HTML表中的大量数据。

  1. <table id="mytable">
  2. <tbody>
  3. </tbody>
  4. <tfoot>
  5. <tr><td id="myfoot"></td></tr>
  6. </tfoot>
  7. </table>
  1. function refreshTable(pager,num)
  2. {
  3. if (!pager)
  4. return;
  5. let page_no = 1;
  6. let tbody = $("#mytable > tbody");
  7. if (tbody.length)
  8. tbody.empty();
  9. for (let i=1; i<=num; i++) {
  10. // Display only items on current page
  11. if (!((i-1) % pager.options.rowsPerPage))
  12. ++page_no;
  13. if (page_no-1 == pager.currentPage) {
  14. tbody.append($("<tr><td>Local row "+i+"</td></tr>"));
  15. }
  16. }
  17. } // refreshTable
  18.  
  19. function populatePager(pager,num)
  20. {
  21. if (!pager)
  22. return;
  23. let page_no = 1;
  24. for (let i=1; i<=num; i++) {
  25. // Add a page number each rowsPerPage rows
  26. if (!((i-1) % pager.options.rowsPerPage)) {
  27. pager.anyPaginator("add");
  28. ++page_no;
  29. }
  30. }
  31. } // populatePager
  32.  
  33. // Initialize paginator in tfoot
  34. let num = 200;
  35. let pager = $("#myfoot").anyPaginator({
  36. mode: 0, // 0: buttons, 1: item range, 2: page number
  37. onClick: function() { refreshTable(pager,num); },
  38. rowsPerPage: 5,
  39. });
  40.  
  41. // Display table contents
  42. refreshTable(pager,num);
  43.  
  44. // Add page numbers
  45. populatePager(pager,num);

3.将您自己的CSS样式应用于分页链接。

  1. .any-paginator-container {
  2. margin-top: 10px;
  3. background: white;
  4. font: 12px Verdana, sans-serif;
  5. }
  6.  
  7. .any-paginator-btn {
  8. margin-right: 1px;
  9. white-space: nowrap;
  10. display: inline-block;
  11. min-width: 20px;
  12. text-align: center;
  13. border-radius:3px;
  14. color: #fc5200;
  15. cursor: pointer;
  16. }
  17.  
  18. .any-paginator-num {
  19. border: 1px solid #fc5200;
  20. }
  21.  
  22. .any-paginator-ellipsis {
  23. border: 1px solid white;
  24. color: #888;
  25. cursor: default;
  26. }
  27.  
  28. .any-paginator-inactive {
  29. color: #aaa;
  30. }
  31.  
  32. .any-paginator-compact {
  33. margin-right: 1px;
  34. white-space: nowrap;
  35. display: inline-block;
  36. min-width: 20px;
  37. text-align: center;
  38. color: #333;
  39. }
  40.  
  41. .any-paginator-goto {
  42. white-space: nowrap;
  43. display: inline-block;
  44. }
  45.  
  46. #anyPaginator_goto_inp {
  47. min-width: 22px;
  48. max-width: 22px;
  49. min-height: unset;
  50. margin-right: 3px;
  51. padding: 1px;
  52. padding-top: 0px;
  53. padding-bottom: 0px;
  54. white-space: nowrap;
  55. display: inline-block;
  56. text-align: center;
  57. font: 12px Verdana, sans-serif;
  58. }
  59.  
  60. #anyPaginator_goto_inp:focus {
  61. outline: none;
  62. }
  63.  
  64. #anyPaginator_goto_btn {
  65. padding: 2px;
  66. white-space: nowrap;
  67. display: inline-block;
  68. cursor: pointer;
  69. }
  70.  
  71. #anyPaginator_goto_btn:hover {
  72. padding: 1px;
  73. border: 1px solid #ddd;
  74. }
  75.  
  76. .noselect {
  77. -webkit-touch-callout: none; /* iOS Safari */
  78. -webkit-user-select: none; /* Safari */
  79. -khtml-user-select: none; /* Konqueror HTML */
  80. -moz-user-select: none; /* Old versions of Firefox */
  81. -ms-user-select: none; /* Internet Explorer/Edge */
  82. user-select: none; /* Currently supported by
  83. Vivaldi, Opera, Chrome, Edge and Firefox */
  84. }

4.这个例子展示了如何处理远程数据。

  1. <table id="mytable">
  2. <tbody>
  3. </tbody>
  4. <tfoot>
  5. <tr><td id="myfoot"></td></tr>
  6. </tfoot>
  7. </table>
  1. let dataSource = "/path/to/remote/data/";
  2.  
  3. function refreshTable(pager,num)
  4. {
  5. if (!pager)
  6. return;
  7. let from = pager.options.rowsPerPage *(pager.currentPage - 1);
  8. let to = from + pager.options.rowsPerPage - 1;
  9. let db_url = dataSource + "remote_select.php?from="+from+"&to="+to;
  10. $.getJSON(db_url)
  11. .done( function(data,textStatus,jqXHR) {
  12. if (textStatus == "success") {
  13. let tbody = $("#mytable > tbody");
  14. if (tbody.length)
  15. tbody.empty();
  16. // Display all items received
  17. for (const i in data)
  18. tbody.append($("<tr><td>"+data[i]+"</td></tr>"));
  19. populatePager(pager,num,pager.currentPage);
  20. }
  21. else
  22. if (textStatus == "error")
  23. console.log("Error: "+jqXHR.status+": "+jqXHR.statusText);
  24. })
  25. .fail(function(jqXHR,textStatus,error) {
  26. console.error("Server error (select):"+jqXHR.responseText);
  27. });
  28. } // refreshTable
  29.  
  30. function populatePager(pager,num,page)
  31. {
  32. if (!pager)
  33. return;
  34. pager.reset();
  35. let page_no = 1;
  36. for (let i=1; i<=num; i++) {
  37. // Add a page number each rowsPerPage rows
  38. if (!((i-1) % pager.options.rowsPerPage)) {
  39. pager.anyPaginator("add");
  40. ++page_no;
  41. }
  42. }
  43. pager.showPage(page);
  44. } // populatePager
  45.  
  46. // Create remote table
  47. let num = 70; // Number of rows
  48. let db_url = dataSource + "remote_create.php?num="+num;
  49. $.getJSON(db_url)
  50. .done( function(data,textStatus,jqXHR) {
  51.  
  52. // Initialize paginator in tfoot
  53. let pager = $("#myfoot").anyPaginator({
  54. mode: 0,
  55. onClick: function() { refreshTable(pager,num); },
  56. rowsPerPage: 5,
  57. prevText: "<",
  58. nextText: ">",
  59. });
  60.  
  61. // Display table contents
  62. refreshTable(pager,num);
  63.  
  64. // Add page numbers
  65. populatePager(pager,num);
  66.  
  67. })
  68.  
  69. .fail(function(jqXHR,textStatus,error) {
  70. console.error("Server error (create):"+jqXHR.responseText);
  71. });

5.所有默认选项。

  1. let pager = $("#myfoot").anyPaginator({
  2.  
  3. // 0: buttons, 1: item range, 2: page number
  4. mode: 0,
  5.  
  6. // If true, hide the paginator if there is only one page
  7. hideIfOne: true,
  8. // Number of items per page
  9. itemsPerPage: 20,
  10.  
  11. // Text in front of page number for mode == 1
  12. pageText: "Page",
  13.  
  14. // Text in front of item range for mode == 2
  15. itemText: "Item",
  16.  
  17. // Text on the "go" button
  18. gotoText: "Go",
  19.  
  20. // Text on the "previous" button, ignored if prevIcon is not null
  21. prevText: "&laquo;",
  22.  
  23. // Text on the "next" button, ignored if nextIcon is not null
  24. nextText: "&raquo;",
  25.  
  26. // Text on the "first" button, ignored if firstIcon is not null
  27. firstText: "|<",
  28.  
  29. // Text on the "last" button, ignored if lastIcon is not null
  30. lastText: ">|",
  31.  
  32. // Icon on the "go" button instead of gotoText
  33. gotoIcon: null,
  34.  
  35. // Icon on the "previous" button instead of prevText
  36. prevIcon: null,
  37.  
  38. // Icon on the "next" button instead of nextText
  39. nextIcon: null,
  40.  
  41. // Icon on the "first" button instead of firstText
  42. firstIcon: null,
  43.  
  44. // Icon on the "last" button instead of lastText
  45. lastIcon: null,
  46.  
  47. // Whether to hide the "goto page" button/input field
  48. hideGoto: false,
  49.  
  50. // Whether to hide the "previous" button
  51. hidePrev: false,
  52.  
  53. // Whether to hide the "next" button
  54. hideNext: false,
  55.  
  56. // Whether to hide the "first" button. Should be "true" if splitLeft == 1
  57. hideFirst: true,
  58.  
  59. // Whether to hide the "last" button. Should be "true" if splitRight == 1
  60. hideLast: true,
  61.  
  62. // Number of split buttons to the left
  63. splitLeft: 3,
  64.  
  65. // Number of split buttons in the middle
  66. splitMiddle: 3,
  67.  
  68. // Number of split buttons to the right
  69. splitRight: 3,
  70.  
  71. });

6.API方法。

  1. // reset the plugin
  2. pager.reset({
  3. // options
  4. });
  5.  
  6. // get the number of pages
  7. pager.numPages();
  8.  
  9. // get the current page
  10. pager.currentPage();
  11.  
  12. // get the number of items
  13. pager.numItems();
  14.  
  15. // set the number of items
  16. pager.numItems(10);
  17.  
  18. // increase/decrease number of items by 1
  19. pager.addItem();
  20. pager.removeItem();
  21.  
  22. // get the current options & values
  23. pager.option();
  24. pager.option(optionName);
  25.  
  26. // update options
  27. pager.option({
  28. // options
  29. });
  30.  
  31. // refresh
  32. pager.refresh();
  33.  
  34. // increase/decrease number of pages by 1
  35. pager.addPage();
  36. pager.removePage();
  37.  
  38. // show page x
  39. pager.showPage(x);

更新日志:

1.0版(2023-04-05)

  • 启用setter中非真值的设置,如零和false;修复一些错误。
  • 使寻呼机的jQuery元素记住寻呼机的属性,以便更灵活地使用。
  • 其他小错误修复和“外观”更改。

2023-04-05

  • CSS轻微更改。

2022-02-08

  • 添加了一些缺少的分号,以使迷你人满意。

2022-02-05

  • CSS的进一步(微小)改进。

2022-02-04

  • 覆盖goto输入字段的css高度。

2022-01-27

  • 将任何分页器或压缩CSS类拆分为两个独立的类。

2022-01-26

  • 错误修复:添加了类any-paginator到btn。
  • 添加了一个“baseId”,以便可以同时拥有多个分页器。

2022-01-21

  • JS和CSS更新

2022-01-18

  • 小改进。

2022-01-14

  • 错误修复
  • CSS调整
  • API更新

2022-01-13

  • 错误修复

预览截图