tablesort-library

tablesort.jsに必要なソートタイプを含めたもの

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/473024/1234887/tablesort-library.js

  1. // ==UserScript==
  2. // @name tablesort-library
  3. // @namespace https://gf.qytechs.cn/
  4. // @version 5.2.1
  5. // @description tablesort.jsに必要なソートタイプを含めたもの
  6. // @author ame-chan
  7. // @copyright (c) Tristen Brown https://github.com/tristen/tablesort
  8. // @license MIT
  9. // @run-at document-idle
  10. // @noframes
  11. // ==/UserScript==
  12. // https://github.com/tristen/tablesort/blob/master/src/tablesort.js
  13. (function () {
  14. function Tablesort(el, options) {
  15. if (!(this instanceof Tablesort)) return new Tablesort(el, options);
  16. if (!el || el.tagName !== 'TABLE') {
  17. throw new Error('Element must be a table');
  18. }
  19. this.init(el, options || {});
  20. }
  21. var sortOptions = [];
  22. var createEvent = function (name) {
  23. var evt;
  24. if (!window.CustomEvent || typeof window.CustomEvent !== 'function') {
  25. evt = document.createEvent('CustomEvent');
  26. evt.initCustomEvent(name, false, false, undefined);
  27. } else {
  28. evt = new CustomEvent(name);
  29. }
  30. return evt;
  31. };
  32. var getInnerText = function (el, options) {
  33. return el.getAttribute(options.sortAttribute || 'data-sort') || el.textContent || el.innerText || '';
  34. };
  35. // Default sort method if no better sort method is found
  36. var caseInsensitiveSort = function (a, b) {
  37. a = a.trim().toLowerCase();
  38. b = b.trim().toLowerCase();
  39. if (a === b) return 0;
  40. if (a < b) return 1;
  41. return -1;
  42. };
  43. var getCellByKey = function (cells, key) {
  44. return [].slice.call(cells).find(function (cell) {
  45. return cell.getAttribute('data-sort-column-key') === key;
  46. });
  47. };
  48. // Stable sort function
  49. // If two elements are equal under the original sort function,
  50. // then there relative order is reversed
  51. var stabilize = function (sort, antiStabilize) {
  52. return function (a, b) {
  53. var unstableResult = sort(a.td, b.td);
  54. if (unstableResult === 0) {
  55. if (antiStabilize) return b.index - a.index;
  56. return a.index - b.index;
  57. }
  58. return unstableResult;
  59. };
  60. };
  61. Tablesort.extend = function (name, pattern, sort) {
  62. if (typeof pattern !== 'function' || typeof sort !== 'function') {
  63. throw new Error('Pattern and sort must be a function');
  64. }
  65. sortOptions.push({
  66. name: name,
  67. pattern: pattern,
  68. sort: sort,
  69. });
  70. };
  71. Tablesort.prototype = {
  72. init: function (el, options) {
  73. var that = this,
  74. firstRow,
  75. defaultSort,
  76. i,
  77. cell;
  78. that.table = el;
  79. that.thead = false;
  80. that.options = options;
  81. if (el.rows && el.rows.length > 0) {
  82. if (el.tHead && el.tHead.rows.length > 0) {
  83. for (i = 0; i < el.tHead.rows.length; i++) {
  84. if (el.tHead.rows[i].getAttribute('data-sort-method') === 'thead') {
  85. firstRow = el.tHead.rows[i];
  86. break;
  87. }
  88. }
  89. if (!firstRow) {
  90. firstRow = el.tHead.rows[el.tHead.rows.length - 1];
  91. }
  92. that.thead = true;
  93. } else {
  94. firstRow = el.rows[0];
  95. }
  96. }
  97. if (!firstRow) return;
  98. var onClick = function () {
  99. if (that.current && that.current !== this) {
  100. that.current.removeAttribute('aria-sort');
  101. }
  102. that.current = this;
  103. that.sortTable(this);
  104. };
  105. // Assume first row is the header and attach a click handler to each.
  106. for (i = 0; i < firstRow.cells.length; i++) {
  107. cell = firstRow.cells[i];
  108. cell.setAttribute('role', 'columnheader');
  109. if (cell.getAttribute('data-sort-method') !== 'none') {
  110. cell.tabindex = 0;
  111. cell.addEventListener('click', onClick, false);
  112. if (cell.getAttribute('data-sort-default') !== null) {
  113. defaultSort = cell;
  114. }
  115. }
  116. }
  117. if (defaultSort) {
  118. that.current = defaultSort;
  119. that.sortTable(defaultSort);
  120. }
  121. },
  122. sortTable: function (header, update) {
  123. var that = this,
  124. columnKey = header.getAttribute('data-sort-column-key'),
  125. column = header.cellIndex,
  126. sortFunction = caseInsensitiveSort,
  127. item = '',
  128. items = [],
  129. i = that.thead ? 0 : 1,
  130. sortMethod = header.getAttribute('data-sort-method'),
  131. sortOrder = header.getAttribute('aria-sort');
  132. that.table.dispatchEvent(createEvent('beforeSort'));
  133. // If updating an existing sort, direction should remain unchanged.
  134. if (!update) {
  135. if (sortOrder === 'ascending') {
  136. sortOrder = 'descending';
  137. } else if (sortOrder === 'descending') {
  138. sortOrder = 'ascending';
  139. } else {
  140. sortOrder = that.options.descending ? 'descending' : 'ascending';
  141. }
  142. header.setAttribute('aria-sort', sortOrder);
  143. }
  144. if (that.table.rows.length < 2) return;
  145. // If we force a sort method, it is not necessary to check rows
  146. if (!sortMethod) {
  147. var cell;
  148. while (items.length < 3 && i < that.table.tBodies[0].rows.length) {
  149. if (columnKey) {
  150. cell = getCellByKey(that.table.tBodies[0].rows[i].cells, columnKey);
  151. } else {
  152. cell = that.table.tBodies[0].rows[i].cells[column];
  153. }
  154. // Treat missing cells as empty cells
  155. item = cell ? getInnerText(cell, that.options) : '';
  156. item = item.trim();
  157. if (item.length > 0) {
  158. items.push(item);
  159. }
  160. i++;
  161. }
  162. if (!items) return;
  163. }
  164. for (i = 0; i < sortOptions.length; i++) {
  165. item = sortOptions[i];
  166. if (sortMethod) {
  167. if (item.name === sortMethod) {
  168. sortFunction = item.sort;
  169. break;
  170. }
  171. } else if (items.every(item.pattern)) {
  172. sortFunction = item.sort;
  173. break;
  174. }
  175. }
  176. that.col = column;
  177. for (i = 0; i < that.table.tBodies.length; i++) {
  178. var newRows = [],
  179. noSorts = {},
  180. j,
  181. totalRows = 0,
  182. noSortsSoFar = 0;
  183. if (that.table.tBodies[i].rows.length < 2) continue;
  184. for (j = 0; j < that.table.tBodies[i].rows.length; j++) {
  185. var cell;
  186. item = that.table.tBodies[i].rows[j];
  187. if (item.getAttribute('data-sort-method') === 'none') {
  188. // keep no-sorts in separate list to be able to insert
  189. // them back at their original position later
  190. noSorts[totalRows] = item;
  191. } else {
  192. if (columnKey) {
  193. cell = getCellByKey(item.cells, columnKey);
  194. } else {
  195. cell = item.cells[that.col];
  196. }
  197. // Save the index for stable sorting
  198. newRows.push({
  199. tr: item,
  200. td: cell ? getInnerText(cell, that.options) : '',
  201. index: totalRows,
  202. });
  203. }
  204. totalRows++;
  205. }
  206. // Before we append should we reverse the new array or not?
  207. // If we reverse, the sort needs to be `anti-stable` so that
  208. // the double negatives cancel out
  209. if (sortOrder === 'descending') {
  210. newRows.sort(stabilize(sortFunction, true));
  211. } else {
  212. newRows.sort(stabilize(sortFunction, false));
  213. newRows.reverse();
  214. }
  215. // append rows that already exist rather than creating new ones
  216. for (j = 0; j < totalRows; j++) {
  217. if (noSorts[j]) {
  218. // We have a no-sort row for this position, insert it here.
  219. item = noSorts[j];
  220. noSortsSoFar++;
  221. } else {
  222. item = newRows[j - noSortsSoFar].tr;
  223. }
  224. // appendChild(x) moves x if already present somewhere else in the DOM
  225. that.table.tBodies[i].appendChild(item);
  226. }
  227. }
  228. that.table.dispatchEvent(createEvent('afterSort'));
  229. },
  230. refresh: function () {
  231. if (this.current !== undefined) {
  232. this.sortTable(this.current, true);
  233. }
  234. },
  235. };
  236. if (typeof module !== 'undefined' && module.exports) {
  237. module.exports = Tablesort;
  238. } else {
  239. window.Tablesort = Tablesort;
  240. }
  241. })();
  242. // https://github.com/tristen/tablesort/blob/master/src/sorts/tablesort.number.js
  243. (function () {
  244. var cleanNumber = function (i) {
  245. return i.replace(/[^\-?0-9.]/g, '');
  246. },
  247. compareNumber = function (a, b) {
  248. a = parseFloat(a);
  249. b = parseFloat(b);
  250. a = isNaN(a) ? 0 : a;
  251. b = isNaN(b) ? 0 : b;
  252. return a - b;
  253. };
  254. window.Tablesort.extend(
  255. 'number',
  256. function (item) {
  257. return (
  258. item.match(/^[-+]?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/) || // Prefixed currency
  259. item.match(/^[-+]?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/) || // Suffixed currency
  260. item.match(/^[-+]?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/)
  261. ); // Number
  262. },
  263. function (a, b) {
  264. a = cleanNumber(a);
  265. b = cleanNumber(b);
  266. return compareNumber(b, a);
  267. },
  268. );
  269. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址