Categories
DHTML JavaScript

Sortable Tables with JavaScript

For years I've fiddled with trying to make my tables sort in JavaScript.  The user can simply click on the column header to make the table sort on that column.  Click again and it sorts the other way.

There were various scripts out there to do it, but I never had the time and need all at once to get one to work.  None seemed to really work well, or they weren't documented well.

Now I've found it. This one works right out of the box. It's well documented on the page, but here's the gist of it;

To make the Javascript available to your pages:

  • Load the sortabletable.js file into your site.
  • Reference the script in the <head> section of your pages.

Now make the following modifications to the pages with tables;

  • Wrap the row that contains your column headers with <thead></thead> tags.
  • Make sure the row with the colum headers used <th></th> tags instead of <td></td>.
  • Wrap the rows that contain your table data with <tbody></tbody> tags.
  • Add an id="MyTable" attribute to the <table> tag for each table you want sorted.  Of course, the id must be unique on the page.
  • Place the sortable table object immediately after each sortable table.  It's shown in the sample code on the site, easy to place with a simple cut and paste, but remember to replace the 'MyTable' string with the id you used in that table's <table> tag.

That's it!  The table now sorts.  Sweet.

Here's where to find it:

While you're there, remember how whoever runs that site pays his or her bills.

If you're not familiar with the very helpful but little used <th><thead><tbody> and <tfoot> tags, Google them.

2009-04-04: Late News: There's a more advanced table sorting method in this frequency-decoder.com article.

2009-08-10: Late News: And now this great script at yoast.com that has alternate row colouring in the javascript, so that it sorts properly.

Categories
Security

Online Hash Generator

This online hash generator will quickly generate MD5, SHA1 and SHA256 hashes.

Not much more to say.

http://hash-it.net/

Categories
Database MySQL

Re-numbering an auto-increment column

An auto-incremented column can become a little cluttered if records are deleted from the table, and you may wish to delete the slack of the sequence in your id column for instance doesn't bear any relationship to the number of records in the table.

Before doing anything about that to a primary key, bear in mind that there are those who say that a primary key should never, ever be changed.  Do you really need to re-number your primary key?

If you really want to do it, read the caveat below before proceeding.

CAVEAT: I haven't actually tested this!

First, be safe and backup your database.

Next copy your table:

CREATE TABLE mytable2 SELECT * FROM mytable1

Now run the following commands, modified as necessary for your situation, on the copy:

ALTER TABLE mytable2 DROP mycolumn;
ALTER TABLE mytable2 ADD mycolumn int(6) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

Have a look at the new table.  Browse the records.  Do you like what you see?  Run as many tests as are appropriate to your situation.  More is better than less.

If you're satisfied, it's time to disable mytable1 and make mytable two current.

Rename mytable1 to mytable1_old, then rename mytable2 to mytable1.

Keep a close an eye on the situation.  If you're not happy, you can revert to mytable1_old or even restore the backup.