Skip to content

Commit

Permalink
Added sorting for table column.
Browse files Browse the repository at this point in the history
  • Loading branch information
wpamitkumar authored and r-brown committed Oct 2, 2020
1 parent b59fc31 commit 3f19b68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion css/ct-public.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion js/ct-public.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3f19b68

Please sign in to comment.