This is

as days pass by, by Stuart Langridge

. Here I write about many things. In the past I wrote about other things but the past is past. I write code for people to play with, I write about my life on Twitter, and I write here.

On I wrote Sortable tables, on the subject of Uncategorized.

I have been exhorted repeatedly to write more content. But if I did, would I be content? Love those puns. :)

Anyway, the Kryogenix House of Javascript Weirdness presents sortable tables, an unobtrusive DHTML library to allow you to make tabular data sortable by clicking on the headers.

martin

It may or may not be the same codebase, but Plone has this out of the box (and has had for a year or so).

martin

It may or may not be the same codebase, but http://www.plone.org has this out of the box (and has had for a year or so).

sil

Jon,

A couple of things, glancing over your altcolor function:
Why not
function altcolor(tbl) {
for (var r=0;r
}

You need to add to the className, not replace it, or you’ll overwrite sortbottom rows and similar. And you already have a table object with its rows attribute; there’s no need to fetch it back with its ID and TRs!

Scott Johnson

That demo was absolutely the fastest sorting of a table on the web that I’ve ever experienced. That blows away any ActiveX garbage just for the simple fact that I don’t have to wait for the ActiveX to load. Bravo! Well done!

john

OK, sorry to hijack the thread, but I did manage to get this working in a rather kludgy way by adding a function call at the end of ts_resortTable. I’d be open to a simpler approach.

function ts_resortTable(lnk) {
...
...
altcolor(table);
span.innerHTML = ARROW;
}

function altcolor(tbl) {
var row=document.getElementById(tbl.id).getElementsByTagName(‘tr‘)
var l=row.length
for (i=0;i
if (i%2 == 0){row[i].className=‘C0‘}
else {r[i].className=‘C1‘;}
}

}

john

I thought this would work as we walk the rows in ts_resortTable but it isn’t:

newRows[i].className = “C” + (i%2);

john

Brilliant! In my tables I’m coloring alternating rows using class=“C0” or “C1” on a row. I can’t figure out how to keep the pattern correct when a sort has been applied. Any ideas?

Jason

It’s Ruby, so you’re roughly 2.4 times more wrong than if it’d have been Python :-)

Adrian Holovaty

Beautiful work, as always. Thanks for sharing.

sil

Javascript is bleedin’ brilliant. Brilliant, I tell you. It got a bad rep from all the status bar scrollers and stuff, but we damned well ought to reclaim it from people doing lame stuff and put it to real work. More powerful than your favourite language, I bet, unless your favourite language is Perl, in which case it’s more readable and usable, or unless it’s Python, in which case I’m wrong. :)

Jason

Stop it, y‘bugger, I want to carry on hating javascript :-)

T.Ullas

Hi


Anyone has readymade dom for paging of data like google style.

Flemming Mahler

sorttable.js is pretty cool, but we’ve been using a hacked version – sorttable_colored.js – available from http://uzilla.net/uzilla/blog/credits.cfm which makes alternating row coloring in the table rows.

Today we just switched to another version – sorttable_colored_cookie.js – which uses cookies to remember the most recent sorting used.

It’s available from:
http://netfactory.dk/technology/markup/javascript/

sil

Flemming: cheers for the pointer. Nice code!

Dave Polaschek

Hmm. It doesn’t seem to work in Safari. That seems a shame. The small static WebFX version with hidden headers and buttons does work, but Safari doesn’t seem to like any of the versions where you directly click the headers.

Ches

how do I tell it not to add the sorting to a column?

j.morgan

This script is amazing!

one question:

Is there an easy way to ignore a row, say by class or id?

trish

i love this script and it works great for my purposes, but a MAC owner told me it doesn't work properly. he said when he clicks on a header to sort, it sorts the first few properly, but then sorts randomly after that. any ideas?

Dean

It seems to have a "problem" on IE6 where if all the contents of a column are the same, you get an ugly error icon in the status bar. Obviously it doesn't change the way the page works, but it makes you look bad.

Apart from that it's great, thanks.

Ecanned

I'm looking for an easy-to-use sortable table that will read the data from and xml file? Anyone know of one?

Thanks

Norman Harebottle

Hello,

I found this to be a REALLY great tool and decided to add proper support for sorting IP Addresses. I just convert a dotted decimal IP address into an integer value and sort on that. It actually works pretty fast considering. It could probably be optimized more but it works for me.

I added the following line in the ts_resortTable(lnk,clid) function at the bottom of the list of other similar statements:

if (itm.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)) sortfn = ts_sort_ip_address;

Then I added the functions here at appropriate spaces for clarity:

function ts_sort_ip_address(a,b) {

aa = ts_aton(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));

bb = ts_aton(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));

if(aa==bb) return 0;

if(aa

Norman Harebottle

function ts_sort_ip_address(a,b) {

aa = ts_aton(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));

bb = ts_aton(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));

if(aa==bb) return 0;

if(aa

Norman Harebottle

Sorry for the multiple posts. Apparently there is a limited length!

And also the function below to finish things out:

function ts_aton($dottedIpStr) {

var result = 0;

var C = [16777216,65536,256,1];

var nums = $dottedIpStr.split(/\./g);

for(i=0;i

Norman Harebottle

// Remainder of function!

for(i=0;i

Norman Harebottle

function ts_aton($dottedIpStr) {

var result = 0;

var C = [16777216,65536,256,1];

var nums = $dottedIpStr.split(/\./g);

for(i=0;i

Norman Harebottle

Ok, you'll just have to e-mail me for the full source! (normanh+spam@gmail.com)

mcchots

Hi

Really cool script.

Is it possible to ignore a certain number of rows at the bottom of the table where totals would go?

sil

mcchots: that's what "sortbottom" is for...

Roy Vincent

Great script! Very usable. I was thinking if there's anyway that it can't while ignoring the case. Currently items with lower case goes AFTER those that starts with an upper case.

Federico

Great script.

Is it possible to use the script if table headers are in a different table?

sil

Federico: no, I'm afraid not. Drop me a mail if you want to talk about that.

Farid

For right sorting IP addresses add this function at the end of script:

function inet_aton(a){

var octets = a.split(/\./);

while (octets.length < 4) octets.splice(1,0,0);

var n = 0;

for (var i = 0; i < 4; i++) n = (n * 256) + parseInt(octets[i]);

return n;

}

And put this after sort_numeric:

sort_ip: function(a,b) {

aa = inet_aton(a[0]);

bb = inet_aton(b[0]);

if (aa==bb) return 0;

if (aa<bb) return -1;

return 1;

},

Farid

For using IP sorting you have to set the class in of this column:

IP address

If you have problems if it mail me : farid D0T adigamov AT gmail D0T com

Farid

For using IP sorting you have to set the class in of this column:

<th class="sorttable_ip">IP address</th>

If you have problems if it mail me : farid D0T adigamov AT gmail D0T com

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.