You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tablesort.js 772B

12345678910111213141516171819202122
  1. export default function initTableSort() {
  2. for (const header of document.querySelectorAll('th[data-sortt-asc]') || []) {
  3. const sorttAsc = header.getAttribute('sortt-asc');
  4. const sorttDesc = header.getAttribute('sortt-desc');
  5. const sorttDefault = header.getAttribute('sortt-default');
  6. header.addEventListener('click', () => {
  7. tableSort(sorttAsc, sorttDesc, sorttDefault);
  8. });
  9. }
  10. }
  11. function tableSort(normSort, revSort, isDefault) {
  12. if (!normSort) return false;
  13. if (!revSort) revSort = '';
  14. const url = new URL(window.location);
  15. let urlSort = url.searchParams.get('sort');
  16. if (!urlSort && isDefault) urlSort = normSort;
  17. url.searchParams.set('sort', urlSort !== normSort ? normSort : revSort);
  18. window.location.replace(url.href);
  19. }