I’ve built this table with a sort function and now I’m also trying to set a class for the selected th
so I can put some arrows with CSS, as sort indicators.
I need a way to set dynamic .asc and .desc classes to my selected th
with JS somewhere on my sortTable()
..
It also needs to be Pure JS.
JavaScript
x
36
36
1
var myData, asc = {'userId':true, 'id':true, 'title':true, 'completed':true};
2
var myRequest = new XMLHttpRequest();
3
myRequest.open('GET', 'https://jsonplaceholder.typicode.com/todos');
4
myRequest.onload = function () {
5
myData = JSON.parse(myRequest.responseText);
6
dataTable(myData);
7
};
8
myRequest.send();
9
10
function dataTable(data) {
11
if (data.length > 0) {
12
var temp = '';
13
data.forEach((u) => {
14
temp += '<tr>';
15
temp += "<td style='text-align: center'>" + u.userId + '</td>';
16
temp += "<td style='text-align: center'>" + u.id + '</td>';
17
temp += '<td>' + u.title + '</td>';
18
temp += "<td style='text-align: center'>" + u.completed + '</td>';
19
temp += "<td>" + '<input type="button" value="Delete" onclick="deleteRow(this)"> <input type="button" value="Edit" onclick="insertToForm(this)">' + '</td></tr>';
20
document.getElementById('data').innerHTML = temp;
21
});
22
}
23
}
24
function sortTable(col){
25
myData.sort(function(a, b) {
26
if(asc[col]){
27
return a[col] > b[col]? 1:-1;
28
}
29
else{
30
return a[col] > b[col]? -1:1;;
31
}
32
});
33
asc[col] = !asc[col];
34
document.getElementById('data').innerHTML = '';
35
dataTable(myData);
36
}
JavaScript
1
38
38
1
.container{
2
display: flex;
3
margin: 25px 10px;
4
}
5
.my-table {
6
border-collapse: collapse;
7
font-size: 0.9em;
8
font-family: sans-serif;
9
min-width: 400px;
10
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
11
margin-right: 15px;
12
}
13
.my-table td, th {
14
padding: 12px 15px;
15
}
16
.my-table thead {
17
background-color: #009879;
18
color: #ffffff;
19
text-align: left;
20
}
21
.my-table th {
22
background-color: #009879;
23
color: #ffffff;
24
text-align: left;
25
cursor: pointer;
26
}
27
.my-table tbody tr {
28
border-bottom: 1px solid #dddddd;
29
}
30
tbody tr input {
31
width: 50px;
32
}
33
.my-table tbody tr:nth-of-type(even) {
34
background-color: #f3f3f3;
35
}
36
.my-table tbody tr:last-of-type {
37
border-bottom: 2px solid #009879;
38
}
JavaScript
1
14
14
1
<table id="table" class="my-table">
2
<thead>
3
<tr>
4
<th id="tbl-head-userid" onclick="sortTable('userId');">UserID</th>
5
<th id="tbl-head-id" onclick="sortTable('id');">ID</th>
6
<th id="tbl-head-title" onclick="sortTable('title');">Title</th>
7
<th id="tbl-head-completed" onclick="sortTable('completed');">Completion</th>
8
<th>Action</th>
9
</tr>
10
</thead>
11
<tbody id="data">
12
13
</tbody>
14
</table>
Advertisement
Answer
I have edited the snipped. arrow buttons will be shown when the <th>
is clicked and the icons on the other headers will be removed
JavaScript
1
48
48
1
var myData, asc = {'userId':true, 'id':true, 'title':true, 'completed':true};
2
var myRequest = new XMLHttpRequest();
3
myRequest.open('GET', 'https://jsonplaceholder.typicode.com/todos');
4
myRequest.onload = function () {
5
myData = JSON.parse(myRequest.responseText);
6
dataTable(myData);
7
};
8
myRequest.send();
9
10
function dataTable(data) {
11
if (data.length > 0) {
12
var temp = '';
13
data.forEach((u) => {
14
temp += '<tr>';
15
temp += "<td style='text-align: center'>" + u.userId + '</td>';
16
temp += "<td style='text-align: center'>" + u.id + '</td>';
17
temp += '<td>' + u.title + '</td>';
18
temp += "<td style='text-align: center'>" + u.completed + '</td>';
19
temp += "<td>" + '<input type="button" value="Delete" onclick="deleteRow(this)"> <input type="button" value="Edit" onclick="insertToForm(this)">' + '</td></tr>';
20
document.getElementById('data').innerHTML = temp;
21
});
22
}
23
}
24
function sortTable(col, e){
25
myData.sort(function(a, b) {
26
if(asc[col]){
27
return a[col] > b[col]? 1:-1;
28
}
29
else{
30
return a[col] > b[col]? -1:1;;
31
}
32
});
33
asc[col] = !asc[col];
34
document.getElementById('data').innerHTML = '';
35
dataTable(myData);
36
var currentTarget = e.currentTarget;
37
38
Array.from(currentTarget.parentElement.children).forEach(function(ele) {
39
40
ele.classList.remove('asc', 'des');
41
})
42
43
if(!asc[col]) {
44
currentTarget.classList.add('asc');
45
} else {
46
currentTarget.classList.add('des');
47
}
48
}
JavaScript
1
60
60
1
.container{
2
display: flex;
3
margin: 25px 10px;
4
}
5
.my-table {
6
border-collapse: collapse;
7
font-size: 0.9em;
8
font-family: sans-serif;
9
min-width: 400px;
10
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
11
margin-right: 15px;
12
}
13
.my-table td, th {
14
padding: 12px 15px;
15
}
16
.my-table thead {
17
background-color: #009879;
18
color: #ffffff;
19
text-align: left;
20
}
21
.my-table th {
22
background-color: #009879;
23
color: #ffffff;
24
text-align: left;
25
cursor: pointer;
26
}
27
.my-table tbody tr {
28
border-bottom: 1px solid #dddddd;
29
}
30
tbody tr input {
31
width: 50px;
32
}
33
.my-table tbody tr:nth-of-type(even) {
34
background-color: #f3f3f3;
35
}
36
.my-table tbody tr:last-of-type {
37
border-bottom: 2px solid #009879;
38
}
39
th {
40
position: relative;
41
}
42
th.asc:before,
43
th.des:after {
44
border: 4px solid transparent;
45
content: "";
46
display: block;
47
height: 0;
48
right: 5px;
49
top: 50%;
50
position: absolute;
51
width: 0;
52
}
53
54
th.asc:before {
55
border-bottom-color: #fff;
56
margin-top: -5px;
57
}
58
th.des:after {
59
border-top-color: #ffff;
60
}
JavaScript
1
14
14
1
<table id="table" class="my-table">
2
<thead>
3
<tr>
4
<th id="tbl-head-userid" onclick="sortTable('userId', event);">UserID</th>
5
<th id="tbl-head-id" onclick="sortTable('id', event);">ID</th>
6
<th id="tbl-head-title" onclick="sortTable('title', event);">Title</th>
7
<th id="tbl-head-completed" onclick="sortTable('completed', event);">Completion</th>
8
<th>Action</th>
9
</tr>
10
</thead>
11
<tbody id="data">
12
13
</tbody>
14
</table>