Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Trying to create a custom sort function for an existing script
Old 10-11-2006, 12:14 AM [Resolved] Trying to create a custom sort function for an existing script
Average Talker

Posts: 20
Name: Chris
Trades: 0
Edit - Got the code from the developer

I need some help modifying a script I've come across from here. It's a great script that allows for dynamic table sorting, my challenge is that it really screws up the appearance of my table which is very reliant on CSS. Basically what's happened is that every th that I've designated as class="sortable" (as per the script) is having the CSS's formatting overwritten. Through looking over some of his sample code I've determined that if I can create a sortable function I can use CSS to customize it.

Example: Instead of using class="sortable" to sort a numeric column I can create a function that does exactly the same thing but name it class="sortable-sortNumber" and then in CSS I can customize it by using th.sortable-sortNumber.

The problem is that I have no idea, even after looking through his code, on how to create my own functions (two for numeric, two for text, one for date). All these features are present however I need to create five new functions so I can customize them.

Here's an example of one of his "custom sort functions" for an IP address.
Code:
/*
   sortIPAddress
   -------------
   
   This custom sort function correctly sorts IP addresses i.e. it checks all of the address parts and not just the first.

   The function is "safe" i.e. non-IP address data (like the word "Unknown") can be passed in and is sorted properly.
*/
function sortIPAddress(a,b) {
        var aa = a[fdTableSort.pos];
        var bb = b[fdTableSort.pos];

        return aa - bb;
}
function sortIPAddressPrepareData(tdNode, innerText) {
        // Get the innerText of the TR nodes
        var aa = innerText;

        // Remove spaces
        aa = aa.replace(" ","");

        // If not an IP address then return -1
        if(aa.search(/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$/) == -1) return -1;

        // Split on the "."
        aa = aa.split(".");

        // If we don't have 4 parts then return -1
        if(aa.length != 4) return -1;
        
        var retVal = "";
        
        // Make all the parts an equal length and create a master integer
        for(var i = 0; i < 4; i++) {
                retVal += (String(aa[i]).length < 3) ? "0000".substr(0, 3 - String(aa[i]).length) + String(aa[i]) : aa[i];
        }
        
        return retVal;
}
If it helps at all here's the actual script itself in the attachment. I know what I'm trying to do is bound to be simple as hell... I just don't know a lick of Java and I need this done in the next couple days on top of a bunch more site coding I'm working with. Thanks a ton for any help you can spare
Attached Files
File Type: zip tablesort.zip (4.1 KB, 0 views)

Last edited by toastysquirrel; 10-11-2006 at 01:35 PM..
toastysquirrel is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-11-2006, 01:29 AM Re: Trying to create a custom sort function for an existing script
Average Talker

Posts: 20
Name: Chris
Trades: 0
The URL code is being annoying for some reason, here's the link:

http//www.frequency-decoder.com/2006/09/16/unobtrusive-table-sort-script-revisited
toastysquirrel is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Trying to create a custom sort function for an existing script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10111 seconds with 13 queries