Skip to content
Snippets Groups Projects
Commit b2bbd291 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms Committed by Rasmus Fuhse
Browse files

allow widgets to be passed to tablesorter, fixes #1307

Closes #1307

Merge request studip/studip!804
parent 0e194a69
No related branches found
No related tags found
No related merge requests found
function enhanceSortableTable(table) { /**
var headers = {}; * This class is used to enhance sortable tables in Stud.IP by using the
$('thead tr:last th', table).each(function(index, element) { * tablesorter plugin.
headers[index] = { *
sorter: $(element).data().sort || false * @see https://mottie.github.io/tablesorter/docs/
}; */
}); class Table
{
/**
* This method will make the given sortable. The table my be given as a
* DOM element or wrapped in a jQuery object.
*
* Additional widgets for the tablesorter may be passed as an array via
* the attribute [data-sort-widgets].
* (see https://mottie.github.io/tablesorter/docs/example-widgets.html)
*
* @param table
*/
static async enhanceSortableTable(table)
{
// Unjquerify table
if (table instanceof jQuery) {
table = table.get(0);
}
if ($('tbody tr[data-sort-fixed]', table).length > 0) { await STUDIP.loadChunk('tablesorter');
$('tbody tr[data-sort-fixed]', table).each(function() {
$(this).data('sort-fixed', { // Iterate over the header columns and determine sorting mechanism
index: $(this).index(), let headers = {};
tbody: $(this).closest('table').find('tbody').index($(this).parent()) $('thead tr:last th', table).each((index, element) => {
}); headers[index] = {
sorter: element.dataset.sort ?? false
};
}); });
$(table)
.on('sortStart', function() { // Handle potential fixed rows
$('tbody tr[data-sort-fixed]', table).each(function() { if ($('tbody tr[data-sort-fixed]', table).length > 0) {
var hidden = $(this).is(':hidden'); $('tbody tr[data-sort-fixed]', table).each(function () {
$(this).data('sort-fixed', {
index: $(this).index(),
tbody: $(this).closest('table').find('tbody').index($(this).parent())
});
});
$(table).on('sortStart', () => {
$('tbody tr[data-sort-fixed]', table).each(function () {
const hidden = $(this).is(':hidden');
$(this).data('sort-hidden', hidden); $(this).data('sort-hidden', hidden);
}); });
}) }).on('sortEnd', () => {
.on('sortEnd', function() { $('tbody tr[data-sort-fixed]', table).detach().each(function () {
$('tbody tr[data-sort-fixed]', table) const pos = $(this).data('sort-fixed');
.detach() if ($(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).length > 0) {
.each(function() { $(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).before(this);
var pos = $(this).data('sort-fixed'); } else {
if ($(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).length > 0) { $(`tbody:eq(${pos.tbody})`, table).append(this);
$(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).before(this); }
} else {
$(`tbody:eq(${pos.tbody})`, table).append(this);
}
if ($(this).data('sort-hidden')) { if ($(this).data('sort-hidden')) {
setTimeout(() => $(this).hide(), 100); setTimeout(() => $(this).hide(), 100);
} }
}); });
}); });
} }
$(table).tablesorter({ // Get additional widgets
headers: headers, const widgets = $(table).data().sortWidgets ?? [];
sortLocaleCompare : true,
sortRestart: true,
widthFixed: false
});
}
const Table = { // Actually activate table sorter
enhanceSortableTable: function (table) { $(table).tablesorter({
STUDIP.loadChunk('tablesorter').then(() => enhanceSortableTable(table)); headers: headers,
sortLocaleCompare : true,
sortRestart: true,
widthFixed: false,
widgets: Array.isArray(widgets) ? widgets : []
});
} }
}; }
export default Table; export default Table;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment