ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
On our Intranet at work I look after a web app that uses a simple html table to display results - nothing complicated, data is retrieved from a SQL database with a simple select statement.
I want to add sorting capability to the table and I have a nice simple Javascript that does it nicely all bar one point - I would like to add an arrow to indicate the sort order.
I also have a complicated Javascript that does add a direction of sort indicator, so i thought I could just lift that bit of code and add into the simple Javascript, but it's beyond me - maybe someone can help - much appreciated.
Here's the simple javascript and html thatworks nicely:
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script>
function sortTable(table, col, reverse) {
var tb = table.tBodies[0], // use `<tbody>` to ignore `<thead>` and `<tfoot>` rows
tr = Array.prototype.slice.call(tb.rows, 0), // put rows into array
i;
reverse = -((+reverse) || -1);
tr = tr.sort(function (a, b) { // sort rows
return reverse // `-1 *` if want opposite order
* (a.cells[col].textContent.trim() // using `.textContent.trim()` for test
.localeCompare(b.cells[col].textContent.trim())
);
});
for(i = 0; i < tr.length; ++i) tb.appendChild(tr[i]); // append each row in order
}
function makeSortable(table) {
var th = table.tHead, i;
th && (th = th.rows[0]) && (th = th.cells);
if (th) i = th.length;
else return; // if no `<thead>` then do nothing
while (--i >= 0) (function (i) {
var dir = 1;
th[i].addEventListener('click', function () {sortTable(table, i, (dir = 1 - dir))});
}(i));
}
function makeAllSortable(parent) {
parent = parent || document.body;
var t = parent.getElementsByTagName('table'), i = t.length;
while (--i >= 0) makeSortable(t[i]);
}
window.onload = function () {makeAllSortable();};
</script>
</head>
<body>
<H1>Test Sorting table</h1>
<table border=1 cellspacing=0>
<thead>
<tr>
<th>Asset No</th>
<th>Description</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<tr>
<td>JKEL03001234</td><td>Emergency Light</td><td>Rm 35</td>
</tr>
<tr>
<td>JKEL03001239</td><td>Emergency Light</td><td>Rm 36</td>
</tr>
<tr>
<td>JKEP01001234</td><td>Emergency Power Off</td><td>Rm 37</td>
</tr>
<tr>
<td>JKED03001234</td><td>Emergency Door</td><td>Rm 38</td>
</tr>
</tbody>
</table>
</body>
</html>
And here's a clip of the code from the more complicated Javascript, I think it's the bit that adds an up/down triangle:
I guess it would be redundant to suggest the clip you added inserts a rule into a style sheet. The unicode in the content field is for up arrow (25b2) and down arrow (25bc) , however there isn't a style sheet included in the code you posted and the rule will need application the element you wish to apply it to.
I guess it would be redundant to suggest the clip you added inserts a rule into a style sheet. The unicode in the content field is for up arrow (25b2) and down arrow (25bc) , however there isn't a style sheet included in the code you posted and the rule will need application the element you wish to apply it to.
I wouldn't call it long. It does what you want so the answer is to understand what you need to do to use it so if you're not a programmer you may have a challenge if you need to modify it. On the other hand if you just want to use it in some page you just need to understand the format it expects from the table.
It looks for a table with the class "js-sort-table" and when a column header is clicked it adds an arrow showing the sort directing. Click it again it changes direction. It handles number, string and date comparison. I added a date column as an example but you can completely rewrite the table and it will work;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.