aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/table.js
blob: a18d7d15884f1dc4649b4df9c83399c0811ea860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function enhanceSortableTable(table) {
    var headers = {};
    $('thead tr:last th', table).each(function(index, element) {
        headers[index] = {
            sorter: $(element).data().sort || false
        };
    });

    if ($('tbody tr[data-sort-fixed]', table).length > 0) {
        $('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', function() {
                $('tbody tr[data-sort-fixed]', table).each(function() {
                    var hidden = $(this).is(':hidden');
                    $(this).data('sort-hidden', hidden);
                });
            })
            .on('sortEnd', function() {
                $('tbody tr[data-sort-fixed]', table)
                    .detach()
                    .each(function() {
                        var pos = $(this).data('sort-fixed');
                        if ($(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).length > 0) {
                            $(`tbody:eq(${pos.tbody}) tr:eq(${pos.index})`, table).before(this);
                        } else {
                            $(`tbody:eq(${pos.tbody})`, table).append(this);
                        }

                        if ($(this).data('sort-hidden')) {
                            setTimeout(() => $(this).hide(), 100);
                        }
                    });
            });
    }

    $(table).tablesorter({
        headers: headers,
        sortLocaleCompare : true,
        sortRestart: true,
        widthFixed: false
    });
}

const Table = {
    enhanceSortableTable: function (table) {
        STUDIP.loadChunk('tablesorter').then(() => enhanceSortableTable(table));
    }
};

export default Table;