forked from ascii-soup/Tablesorter-Static-Row-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tablesorter.staticrow.js
76 lines (68 loc) · 2.73 KB
/
jquery.tablesorter.staticrow.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
*
* StaticRow widget for jQuery TableSorter 2.0
* Version 1.0
*
* Copyright (c) 2011 Nils Luxton
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
$.tablesorter.addWidget({
// Give the new Widget an ID to be used in the tablesorter() call, as follows:
// $('#myElement').tablesorter({ widgets: ['zebra','staticRow'] });
id: 'staticRow',
// "Format" is run on all widgets once when the tablesorter has finished initialising,
// and then again every time a sort has finished.
format: function(table) {
// Use a property of the function to determine
// whether this is the first run of "Format"
// (i.e. is this the table's default starting position,
// or has it been sorted?)
if (typeof $(table).data('hasSorted') == 'undefined')
{
$(table).data('hasSorted', true); // This will force us into the "else" block the next time "Format" is run
// "Index" the static rows, saving their current (starting)
// position in the table inside a data() param on the
// <tr> element itself for later use.
$('tbody .static', table).each(function() {
$(this).data('tableindex', $(this).index());
});
}
else
{
// Loop the static rows, moving them to their
// original "indexed" position, and keep doing
// this until no more re-shuffling needs doing
var hasShuffled = true;
while (hasShuffled)
{
hasShuffled = false;
$('tbody .static', table).each(function() {
var targetIndex = $(this).data('tableindex');
if (targetIndex != $(this).index())
{
hasShuffled = true;
var thisRow = $(this).detach();
var numRows = $('tbody tr', table).length;
// Are we trying to be the last row?
if (targetIndex >= numRows)
{
thisRow.appendTo($('tbody', table));
}
// Are we trying to be the first row?
else if (targetIndex == 0)
{
thisRow.prependTo($('tbody', table));
}
// No, we want to be somewhere in the middle!
else
{
thisRow.insertBefore($('tbody tr:eq(' + targetIndex + ')', table));
}
}
});
}
}
}
});