Wednesday 28 August 2013

isBlank()

Another brilliant little bit of code to complement empty() is isBlank():

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

Monday 12 August 2013

DataTables (custom) sort by month name only

I use DataTables a lot, I think they're brilliant and I'm slowly getting to understand their intricacies. Anyway, I had a need to sort a column by month name only so I came up with this:

var monthNames = ["January", "February", "March", 
    "April", "May", "June", 
    "July", "August", "September", 
    "October", "November", "December"];
jQuery.fn.dataTableExt.oSort['month-name-asc'] = function (x, y) {
    var xPos, yPos;
    jQuery.each(monthNames, function (k, v) {
        if (x == v) xPos = k;
        if (y == v) yPos = k
    });
    return ((xPos < yPos) ? -1 : ((xPos > yPos) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['month-name-desc'] = function (x, y) {
    var xPos, yPos;
    jQuery.each(monthNames, function (k, v) {
        if (x == v) xPos = k
        if (y == v) yPos = k
    });
    return ((xPos > yPos) ? -1 : ((xPos < yPos) ? 1 : 0));
};

Simply add "sType": "month-name" to the column def and away you go!