Client-side sorting and pagination of an apex:pageBlockTable – more on sorting

OK so things looked a little too good to be true in Client-side sorting and pagination of an apex:pageBlockTable and I’ve had to do a bit more work to get the sorting working for datetime, date and currency columns. The problem is of course that the client-side JavaScript only has the formatted data available not the underlying (typed) data, and many formats are possible.

Below is my first cut at handling datetime, date and currency columns for the “English (United States)” locale. To reduce configuration, the type of a column is automatically detected rather than needing to be configured.

var j$ = jQuery.noConflict();

var sorting = {
    
    // Determine the format the column values are in
    typeDetect: function (a) {
        if (a) {
            if (a.match(/\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2} (AM|PM)/)) {
                return 'sf-datetime';
            } else if (a.match(/\d{1,2}\/\d{1,2}\/\d{4}/)) {
                return 'sf-date';
            } else if (a.match(/\(?\\$[\d\,\.]+\)?/)) {
                return 'sf-currency';
            }
        }
        return null;
    },
    
    // e.g. '8/17/2012 6:37 AM'
    datetimePre: function (a) {
        var b = a.match(/(\d{1,2})\/(\d{1,2})\/(\d{4}) (\d{1,2}):(\d{1,2}) (AM|PM)/);
        if (b && b.length == 7) {
            var month = b[1], day = b[2], year = b[3], hour = b[4], min = b[5], ap = b[6];
            if (hour == '12') hour = '0';
            if (ap == 'PM') hour = parseInt(hour, 10) + 12;
            if (month.length == 1) month = '0' + month;
            if (day.length == 1) day = '0' + day;
            if (hour.length == 1) hour = '0' + hour;
            if (min.length == 1) min = '0' + min;
            return year + month + day + hour + min;
        } else {
            return '';
        }
    },
    
    // e.g. '8/17/2012'
    datePre: function (a) {
        var b = a.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
        if (b && b.length == 4) {
            var month = b[1], day = b[2], year = b[3];
            if (month.length == 1) month = '0' + month;
            if (day.length == 1) day = '0' + day;
            return year + month + day;
        } else {
            return '';
        }
    },
    
    // e.g. '$1,019.05' or '($500.00)'
    currencyPre: function (a) {
        var b = a.match(/(\()?(\\$)([\d\,\.]+)(\))?/);
        if (b && b.length == 5) {
            return parseFloat((b[1] == '(' ? '-' : '') + b[3].replace(/\,/g, ''));
        } else {
            return 0;
        }
    },
    
    asc: function (a, b) {
       return a - b;
    },
    
    desc: function (a, b) {
       return b - a;
    }
}

// Register extra type detection
j$.fn.dataTableExt.aTypes.unshift(sorting.typeDetect);

// Register extra sorting functions
j$.extend(j$.fn.dataTableExt.oSort, {
    
    'sf-datetime-pre': sorting.datetimePre,
    'sf-datetime-asc': sorting.asc,
    'sf-datetime-desc': sorting.desc,
   
    'sf-date-pre': sorting.datePre,
    'sf-date-asc': sorting.asc,
    'sf-date-desc': sorting.desc,
   
    'sf-currency-pre': sorting.currencyPre,
    'sf-currency-asc': sorting.asc,
    'sf-currency-desc': sorting.desc
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s