Jump to content


Photo

[Resolved] Trying to create a custom sort function for an existing script


  • Please log in to reply
1 reply to this topic

#1 toasty

toasty
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 11 October 2006 - 02:59 AM

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.
/*
   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 :D

[attachment deleted by admin]

#2 toasty

toasty
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 11 October 2006 - 04:18 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users