I’m trying to sort rows in alphabetical order based on which column header is clicked using jQuery. It works fairly fine when debugging except that it doesn’t actually switch the rows in the HTML and so it doesn’t display a sorted table on the webpage. I’m using Thymeleaf th:text to populate the table body rows but for the sake of this example, I hardcoded some values. You can run it here: https://jsfiddle.net/tg2khrd4
Javascript:
JavaScript
x
36
36
1
var table = $("#table");
2
3
$("#subject, #from, #id")
4
.wrapInner('<span title="sort this column"/>')
5
.each(function () {
6
var th = $(this),
7
thIndex = th.index(),
8
inverse = false;
9
10
th.click(function () {
11
table
12
.find("tbody")
13
.find("td")
14
.filter(function () {
15
return $(this).index() === thIndex;
16
})
17
.sort(
18
function (a, b) {
19
return $.text([a]) > $.text([b])
20
? inverse
21
? -1
22
: 1
23
: inverse
24
? 1
25
: -1;
26
},
27
function () {
28
// parentNode is the element we want to move
29
return this.parentNode;
30
}
31
);
32
33
inverse = !inverse;
34
});
35
});
36
HTML:
JavaScript
1
38
38
1
<table class="table table-hover" id="table" style="background-color:#fff;border: 1px solid #cccccc">
2
<thead style="background-color:#981e32;">
3
<tr>
4
<td class="tdsubj" id="id" style="padding:5px;">Id
5
</td>
6
<td class="tdsubj" id="subject" style="padding:5px;">
7
Subject
8
</td>
9
<td class="tdsubj" id="from" style="padding:5px;">
10
From
11
</td>
12
<td class="tdsubj" id="date" style="padding:5px;">
13
Date
14
</td>
15
</tr>
16
</thead>
17
<tbody>
18
<tr>
19
<td>1</td>
20
<td>Hello</td>
21
<td>Thor</td>
22
<td>2020-10-19</td>
23
</tr>
24
<tr>
25
<td>2</td>
26
<td>Dinos Suck</td>
27
<td>Meteor</td>
28
<td>2020-9-5</td>
29
</tr>
30
<tr>
31
<td>3</td>
32
<td>Big Ben won't stop ringing</td>
33
<td>The Queen</td>
34
<td>2020-8-19</td>
35
</tr>
36
</tbody>
37
</table>
38
Advertisement
Answer
Once the td
sorted… You just have to loop throught it and append it’s parent tr
in the table…
JavaScript
1
51
51
1
var table = $("#table");
2
3
$("#subject, #from, #id")
4
// .wrapInner('<span title="sort this column"/>')
5
.each(function () {
6
var th = $(this),
7
thIndex = th.index(),
8
inverse = false;
9
10
11
th.click(function () {
12
let test = table
13
.find("tbody")
14
.find("td")
15
.filter(function () {
16
return $(this).index() === thIndex;
17
})
18
.sort(
19
function (a, b) {
20
return $.text([a]) > $.text([b])
21
? inverse
22
? -1
23
: 1
24
: inverse
25
? 1
26
: -1;
27
}
28
29
// That is useless...
30
/*function () {
31
// parentNode is the element we want to move
32
console.log(this.parentNode)
33
return this.parentNode;
34
}*/
35
);
36
37
// Added to demonstrate the sorting works
38
console.clear()
39
test.each(function () {
40
console.log(this.innerText);
41
});
42
43
// Now to apply the sorting on the DOM
44
// Find the tr containing it and append it to the table.
45
test.each(function () {
46
table.append($(this).parent("tr"))
47
});
48
49
inverse = !inverse;
50
});
51
});
JavaScript
1
38
38
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<table class="table table-hover" id="table" style="background-color:#fff;border: 1px solid #cccccc">
3
<thead style="background-color:#981e32;">
4
<tr>
5
<td class="tdsubj" id="id" style="padding:5px;">Id
6
</td>
7
<td class="tdsubj" id="subject" style="padding:5px;">
8
Subject
9
</td>
10
<td class="tdsubj" id="from" style="padding:5px;">
11
From
12
</td>
13
<td class="tdsubj" id="date" style="padding:5px;">
14
Date
15
</td>
16
</tr>
17
</thead>
18
<tbody>
19
<tr>
20
<td>1</td>
21
<td>Hello</td>
22
<td>Thor</td>
23
<td>2020-10-19</td>
24
</tr>
25
<tr>
26
<td>2</td>
27
<td>Dinos Suck</td>
28
<td>Meteor</td>
29
<td>2020-9-5</td>
30
</tr>
31
<tr>
32
<td>3</td>
33
<td>Big Ben won't stop ringing</td>
34
<td>The Queen</td>
35
<td>2020-8-19</td>
36
</tr>
37
</tbody>
38
</table>