forked from padolsey-archive/jquery.fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
104 lines (88 loc) · 3.05 KB
/
demo.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sort plugin for jQuery</title>
</head>
<body>
<h1>Demo</h1>
<p>Click on the headers (fruit/quantity).</p>
<table>
<thead>
<tr>
<th>Fruit</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>Grape</td>
<td>15</td>
</tr>
<tr>
<td>Apple</td>
<td>4</td>
</tr>
<tr>
<td>Banana</td>
<td>88</td>
</tr>
<tr>
<td>Orange</td>
<td>11</td>
</tr>
<tr>
<td>Melon</td>
<td>21</td>
</tr>
<tr>
<td>Tomato</td>
<td>36</td>
</tr>
</tbody>
</table>
<button>Click to sort the list below</button>
<ul>
<li>Lamborghini</li>
<li>Farrari</li>
<li>Masarati</li>
<li>Aston Martin</li>
<li>Porche</li>
</ul>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="jquery.sortElements.js"></script>
<script>
var th = jQuery('th'),
li = jQuery('li'),
inverse = false;
th.click(function(){
var header = $(this),
index = header.index();
header
.closest('table')
.find('td')
.filter(function(){
return $(this).index() === index;
})
.sortElements(function(a, b){
a = $(a).text();
b = $(b).text();
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 :
inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;
});
$('button').click(function(){
li.sortElements(function(a, b){
return $(a).text() > $(b).text() ? 1 : -1;
});
});
</script>
</body>
</html>