| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>Stupid jQuery table sort (complex example)</title> |
| •5 |
<script src="/usr/share/javascript/jquery/jquery.min.js"></script> |
| 6 |
<script src="/usr/share/javascript/jquery-stupidtable/stupidtable.js?dev"></script> |
| 7 |
<script> |
| 8 |
$(function(){ |
| 9 |
// Helper function to convert a string of the form "Mar 15, 1987" into |
| 10 |
// a Date object. |
| 11 |
var date_from_string = function(str){ |
| 12 |
var months = ["jan","feb","mar","apr","may","jun","jul", |
| 13 |
"aug","sep","oct","nov","dec"]; |
| 14 |
var pattern = "^([a-zA-Z]{3})\\s*(\\d{2}),\\s*(\\d{4})$"; |
| 15 |
var re = new RegExp(pattern); |
| 16 |
var DateParts = re.exec(str).slice(1); |
| 17 |
|
| 18 |
var Year = DateParts[2]; |
| 19 |
var Month = $.inArray(DateParts[0].toLowerCase(), months); |
| 20 |
var Day = DateParts[1]; |
| 21 |
return new Date(Year, Month, Day); |
| 22 |
} |
| 23 |
|
| 24 |
var moveBlanks = function(a, b) { |
| 25 |
if ( a < b ){ |
| 26 |
if (a == "") |
| 27 |
return 1; |
| 28 |
else |
| 29 |
return -1; |
| 30 |
} |
| 31 |
if ( a > b ){ |
| 32 |
if (b == "") |
| 33 |
return -1; |
| 34 |
else |
| 35 |
return 1; |
| 36 |
} |
| 37 |
return 0; |
| 38 |
}; |
| 39 |
var moveBlanksDesc = function(a, b) { |
| 40 |
// Blanks are by definition the smallest value, so we don't have to |
| 41 |
// worry about them here |
| 42 |
if ( a < b ) |
| 43 |
return 1; |
| 44 |
if ( a > b ) |
| 45 |
return -1; |
| 46 |
return 0; |
| 47 |
}; |
| 48 |
|
| 49 |
var table = $("table").stupidtable({ |
| 50 |
"date":function(a,b){ |
| 51 |
// Get these into date objects for comparison. |
| 52 |
|
| 53 |
aDate = date_from_string(a); |
| 54 |
bDate = date_from_string(b); |
| 55 |
|
| 56 |
return aDate - bDate; |
| 57 |
}, |
| 58 |
"moveBlanks": moveBlanks, |
| 59 |
"moveBlanksDesc": moveBlanksDesc, |
| 60 |
}); |
| 61 |
|
| 62 |
table.on("beforetablesort", function (event, data) { |
| 63 |
// data.column - the index of the column sorted after a click |
| 64 |
// data.direction - the sorting direction (either asc or desc) |
| 65 |
$("#msg").text("Sorting index " + data.column) |
| 66 |
}); |
| 67 |
|
| 68 |
table.on("aftertablesort", function (event, data) { |
| 69 |
var th = $(this).find("th"); |
| 70 |
th.find(".arrow").remove(); |
| 71 |
var dir = $.fn.stupidtable.dir; |
| 72 |
|
| 73 |
var arrow = data.direction === dir.ASC ? "↑" : "↓"; |
| 74 |
th.eq(data.column).append('<span class="arrow">' + arrow +'</span>'); |
| 75 |
}); |
| 76 |
|
| 77 |
$("tr").slice(1).click(function(){ |
| 78 |
$(".awesome").removeClass("awesome"); |
| 79 |
$(this).addClass("awesome"); |
| 80 |
}); |
| 81 |
|
| 82 |
}); |
| 83 |
</script> |
| 84 |
<style type="text/css"> |
| 85 |
table { |
| 86 |
border-collapse: collapse; |
| 87 |
} |
| 88 |
th, td { |
| 89 |
padding: 5px 10px; |
| 90 |
border: 1px solid #999; |
| 91 |
} |
| 92 |
th { |
| 93 |
background-color: #eee; |
| 94 |
} |
| 95 |
th[data-sort]{ |
| 96 |
cursor:pointer; |
| 97 |
} |
| 98 |
tr.awesome{ |
| 99 |
color: red; |
| 100 |
} |
| 101 |
#msg { |
| 102 |
color: green; |
| 103 |
} |
| 104 |
</style> |
| 105 |
</head> |
| 106 |
|
| 107 |
<body> |
| 108 |
|
| 109 |
<h1>Stupid jQuery table sort! (complex example)</h1> |
| 110 |
|
| 111 |
<p>This example showcases several of the more advanced features, including specifying sort values, custom data types and callbacks. View the source of this file or see the <a href="http://joequery.github.com/Stupid-Table-Plugin/">documentation</a> for more details on how to implement them.</p> |
| 112 |
|
| 113 |
<p id="msg"> </p> |
| 114 |
|
| 115 |
<table> |
| 116 |
<thead> |
| 117 |
<tr> |
| 118 |
<th data-sort="int">int</th> |
| 119 |
<th data-sort="int">int</th> |
| 120 |
<th data-sort="float" data-sort-default="desc">float</th> |
| 121 |
<th data-sort="moveBlanks" data-sort-desc="moveBlanksDesc">string</th> |
| 122 |
<th data-sort="string-ins">case</th> |
| 123 |
<th>Can't sort me!</th> |
| 124 |
<th data-sort="date">date</th> |
| 125 |
<th data-sort="int">Letter frequency</th> |
| 126 |
</tr> |
| 127 |
</thead> |
| 128 |
<tbody> |
| 129 |
<tr> |
| 130 |
<td>15</td> |
| 131 |
<td>15</td> |
| 132 |
<td>-.18</td> |
| 133 |
<td>banana</td> |
| 134 |
<td>Homer</td> |
| 135 |
<td>arbitrary</td> |
| 136 |
<td>Sep 15, 2002</td> |
| 137 |
<td data-sort-value="0">E</td> |
| 138 |
</tr> |
| 139 |
<tr class="awesome"> |
| 140 |
<td>95</td> |
| 141 |
<td>95</td> |
| 142 |
<td>36</td> |
| 143 |
<td></td> |
| 144 |
<td>purple</td> |
| 145 |
<td>pointless</td> |
| 146 |
<td>Aug 07, 2004</td> |
| 147 |
<td data-sort-value="1">T</td> |
| 148 |
</tr> |
| 149 |
<tr> |
| 150 |
<td>2</td> |
| 151 |
<td>2</td> |
| 152 |
<td>-152.5</td> |
| 153 |
<td></td> |
| 154 |
<td>is</td> |
| 155 |
<td>silly</td> |
| 156 |
<td>Mar 15, 1986</td> |
| 157 |
<td data-sort-value="2">A</td> |
| 158 |
</tr> |
| 159 |
<tr> |
| 160 |
<td>-53</td> |
| 161 |
<td>-53</td> |
| 162 |
<td>88.5</td> |
| 163 |
<td>hello</td> |
| 164 |
<td>a</td> |
| 165 |
<td>eccentric</td> |
| 166 |
<td>Feb 27, 2086</td> |
| 167 |
<td data-sort-value="3">O</td> |
| 168 |
</tr> |
| 169 |
<tr> |
| 170 |
<td>195</td> |
| 171 |
<td>195</td> |
| 172 |
<td>-858</td> |
| 173 |
<td>orange</td> |
| 174 |
<td>fruit</td> |
| 175 |
<td>garbage</td> |
| 176 |
<td>Mar 15, 1986</td> |
| 177 |
<td data-sort-value="4">I</td> |
| 178 |
</tr> |
| 179 |
</tbody> |
| 180 |
</table> |
| 181 |
|
| 182 |
</body> |
| 183 |
</html> |