enqueue js sort table

This commit is contained in:
Lem Smyth 2022-02-19 16:56:57 -06:00
parent 31c66404f9
commit 04ad7a8746
2 changed files with 94 additions and 4 deletions

76
admin/js/table-sort.js Normal file
View file

@ -0,0 +1,76 @@
jQuery(document).ready(function($) {
var compare = {
name: function(a, b) {
a = a.replace(/^@/, '') && a.replace(/-/g, '');
b = b.replace(/^@/, '') && b.replace(/-/g, '');
if (a < b) {
return -1;
} else {
return a > b ? 1 : 0;
}
},
lbryurl: function(a, b) {
a = a.replace(/^lbry:\/\/@/i, '') && a.replace(/#[a-zA-Z0-9]+/, '') && a.replace(/-/g, '');
b = b.replace(/^lbry:\/\/@/i, '') && b.replace(/#[a-zA-Z0-9]+/, '') && b.replace(/-/g, '');
if (a < b) {
return -1;
} else {
return a > b ? 1 : 0;
}
},
amount: function(a, b) {
a = a.split('.');
b = b.split('.');
a = Number(a[0]) + Number(a[1]);
b = Number(b[0]) + Number(b[1]);
return a - b;
},
number: function(a, b) {
a = Number(a);
b = Number(b);
return a - b;
},
date: function(a, b) {
a = new Date(a);
b = new Date(b);
return a - b;
}
};
$('.lbry-channel-table').each(function() {
var $table = $(this);
var $tbody = $table.find('tbody');
var $controls = $table.find('th');
var rows = $tbody.find('tr').toArray();
$controls.on('click', function() {
var $header = $(this);
var order = $header.data('sort');
var column;
if ($header.is('.ascending') || $header.is('.descending')) {
$header.toggleClass('ascending descending');
$tbody.append(rows.reverse());
} else {
$header.addClass('ascending');
$header.siblings().removeClass('ascending descending');
if (compare.hasOwnProperty(order)) {
column = $controls.index(this);
rows.sort(function(a, b) {
a = $(a).find('td').eq(column).text();
b = $(b).find('td').eq(column).text();
return compare[order](a, b);
});
$tbody.append(rows);
}
}
});
});
});

View file

@ -51,6 +51,20 @@ class LBRY_Admin
}
}
add_action( 'admin_enqueue_scripts', 'load_admin_stylesheet' );
// Admin JS enqueue
function load_admin_script() {
if ( ( $_GET['page'] == 'lbrypress') && ( $_GET['tab'] == 'channels' ) ) {
wp_enqueue_script(
'lbry-table-sort',
plugins_url( '/admin/js/table-sort.js', LBRY_PLUGIN_FILE ),
array('jquery'),
LBRY_VERSION,
true
);
}
}
add_action( 'admin_enqueue_scripts', 'load_admin_script' );
// Admin Error Notices
function lbry_plugin_not_configured_notice() {
@ -258,10 +272,10 @@ class LBRY_Admin
<table class="lbry-channel-table">
<thead>
<tr>
<th>Channel</th>
<th>LBRY URL</th>
<th>Posts</th>
<th colspan="2">Supports</th>
<th data-sort="name">Channel</th>
<th data-sort="lbryurl">LBRY URL</th>
<th data-sort="number">Posts</th>
<th data-sort="amount" colspan="2">Supports</th>
</tr>
</thead>
<tbody>