A Slightly Enhanced TableKit07 Jul 2010
I won’t go into extensive detail on why this is awesome. Here’s a little demo to play with though, to get the point. Click on column headers (in-line editing is not included for this, but you can find more about that at the link above):
|First Name||Last Name||Age||Teacher|
<table class="sortable resizable" width="500">
At this point, the table should be sortable, and you should be able to resize the columns by dragging the barrier between the column headers. You’ll notice alternating row colors remain alternating. All the formatting (including sort color, the up/down arrows, alternating rows) are all defined as CSS properties, so you can modify them easily. We’ll get to the CSS later on.
While that’s cool (and it IS damn cool, admit it), it isn’t why I’m bothering to stay up late and write a post. I extended this library to give it a new property – “linkable”. It combines the “ConvertTableRowtoHyperlink” (CTRtH) script published a few years back and rolls it into the function of TableKit. The script will also scan all rows of a given table for the first link it comes across. It then creates a function which highlights the row on mouseOver, returns to previous state on mouseOut (preserving the pretty alternating colors created by TableKit). Clicking anywhere on the row will take you to the link. If it finds more than one link, it takes the one furthest to the left. Personally, I think lots of links in a single table isn’t the best plan, but if you want it to give up on rows with more than one link rather than picking the furthest left, there’s instructions on line 90 of the script on how to do this.
Added benefit! In the old CTRtH, you had to explicitly give the ID of each table, invoke separately, and ask the script to do the row conversion. This broke with a number of things, including AJAX updates (ended up including the JS in a partial on one project). Mostly though, it meant I couldn’t have a nice, clean table with TableKit and CTRtH running in tandem. So, I spent a flight from DC to Denver figuring out what was conflicting where, and getting the two to play nicely. Now you have an extended TableKit, which will also happily convert table rows to links when given the property “linkable”, like this:
<table class="sortable resizable linkable" width="600">
|First Name||Last Name||Age||Search Engine|
Alright, down to business.
2) Make sure you add sortable, resizable, or linkable to the CSS class of any table you want to behave this way. You can give it an ID, but you don’t have to. However, like with many JS functions, if you have multiple elements with the same ID, you’ll only affect the first one. If you don’t give it an ID, one will be assigned in sequential order.
3) Here’s what you’ll need to add to your CSS to make it look like mine and behave as you’d expect. You’ll almost certainly want to change the colors, but other than that you should be good, I’ve tried to keep the CSS as minimal as possible so as not to interfere with your other stuff.. Notice the up and down arrows are also just background elements, so point them at your own up.png/down.png location.