sorter.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. // TODO: tablesorter shouldn't sort already sorted columns
  3. // eslint-disable-next-line no-unused-vars
  4. function initTableSorter(tabid) {
  5. var $table;
  6. var opts;
  7. switch (tabid) {
  8. case 'statustabs_queries':
  9. $table = $('#serverStatusQueriesDetails');
  10. opts = {
  11. sortList: [[3, 1]],
  12. headers: {
  13. 1: {
  14. sorter: 'fancyNumber'
  15. },
  16. 2: {
  17. sorter: 'fancyNumber'
  18. }
  19. }
  20. };
  21. break;
  22. }
  23. $table.tablesorter(opts);
  24. $table.find('tr').first().find('th').append('<div class="sorticon"></div>');
  25. }
  26. $(function () {
  27. $.tablesorter.addParser({
  28. id: 'fancyNumber',
  29. is: function is(s) {
  30. return /^[0-9]?[0-9,\\.]*\s?(k|M|G|T|%)?$/.test(s);
  31. },
  32. format: function format(s) {
  33. var num = jQuery.tablesorter.formatFloat(s.replace(Messages.strThousandsSeparator, '').replace(Messages.strDecimalSeparator, '.'));
  34. var factor = 1;
  35. switch (s.charAt(s.length - 1)) {
  36. case '%':
  37. factor = -2;
  38. break;
  39. // Todo: Complete this list (as well as in the regexp a few lines up)
  40. case 'k':
  41. factor = 3;
  42. break;
  43. case 'M':
  44. factor = 6;
  45. break;
  46. case 'G':
  47. factor = 9;
  48. break;
  49. case 'T':
  50. factor = 12;
  51. break;
  52. }
  53. return num * Math.pow(10, factor);
  54. },
  55. type: 'numeric'
  56. });
  57. $.tablesorter.addParser({
  58. id: 'withinSpanNumber',
  59. is: function is(s) {
  60. return /<span class="original"/.test(s);
  61. },
  62. format: function format(s, table, html) {
  63. var res = html.innerHTML.match(/<span(\s*style="display:none;"\s*)?\s*class="original">(.*)?<\/span>/);
  64. return res && res.length >= 3 ? res[2] : 0;
  65. },
  66. type: 'numeric'
  67. });
  68. });