From 3f19b682db458a8633a035f671a73e5d73a139d1 Mon Sep 17 00:00:00 2001 From: Amit Dudhat Date: Thu, 1 Oct 2020 14:54:48 +0530 Subject: [PATCH] Added sorting for table column. --- css/ct-public.css | 3 ++- js/ct-public.js | 14 +++++++++++++- readme.txt | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/css/ct-public.css b/css/ct-public.css index 527b161..804bfae 100755 --- a/css/ct-public.css +++ b/css/ct-public.css @@ -15,7 +15,8 @@ table.credit-tracker-default { table.credit-tracker-default thead tr th { text-transform: uppercase; - background: #e2e2e2; + background: #e2e2e2; + cursor: pointer; } table.credit-tracker-default tfoot tr th, table.credit-tracker-default tfoot tr td { diff --git a/js/ct-public.js b/js/ct-public.js index bd4e7ad..a049626 100755 --- a/js/ct-public.js +++ b/js/ct-public.js @@ -4,6 +4,18 @@ /* 'credit_tracker_table' styling */ $("#credit-tracker-table tr:nth-child(odd)").addClass("odd-row"); $("#credit-tracker-table td:first-child, table th:first-child").addClass("first"); - $("#credit-tracker-table td:last-child, table th:last-child").addClass("last"); + $("#credit-tracker-table td:last-child, table th:last-child").addClass("last"); + + const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent; + const comparer = (idx, asc) => (a, b) => ((v1, v2) => + v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2) + )(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)); + document.querySelectorAll('#credit-tracker-table th').forEach(th => th.addEventListener('click', (() => { + const table = th.closest('table'); + const tbody = table.querySelector('tbody'); + Array.from(table.querySelectorAll('tbody tr')) + .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc)) + .forEach(tr => tbody.appendChild(tr) ); + }))); }); }(jQuery)); diff --git a/readme.txt b/readme.txt index 51dc08a..c12fc0e 100644 --- a/readme.txt +++ b/readme.txt @@ -1,5 +1,5 @@ === Plugin Name === -Contributors: labs64 +Contributors: labs64, wpamitkumar Tags: credit, attribution, legal, copyright, owner, author, media library, media, image, photo, license, royalty-free, RF, Creative Commons, stock, attachment, flickr, fotolia, unsplash, bildnachweis, impressum, imprint, microdata, NetLicensing Requires at least: 4.0 Tested up to: 5.5.1