开发者

jQuery Table Sorting by Attr e.g. Title

Does anyone know of a jQuery plugin that allows table sorting when clicking on the header, but checks say the title of a cell and sorts by that before looking at the contents of the开发者_如何学C cell?

I have some data in my cells such as 3h 5m and 5m, most sorting plugins get these in the wrong order as it sorts it alphanumerically because it doesn't understand the value.

I am sure somewhere I have seem a plugin where you put the raw data in the title, and the plugin actually sorted based on that data rather than the info in the cell.

Many thanks for your time,


Using jQuery dataTables and the following custom sorting function (you may have to expand it if your "last seen" has more than just days/hours/minutes):

jQuery.fn.dataTableExt.oSort['mytime-asc']  = function(a,b) {
    var regex = /\s*(?:(\d+)d)?\s*(?:(\d+)h)?\s*(?:(\d+)m)?\s*/;
    var m;

    m = a.match(regex);
    var x = (m[1] ? parseInt(m[1]) : 0) * 1440 + (m[2] ? parseInt(m[2]) : 0) * 60 + (m[3] ? parseInt(m[3]) : 0);

    m = b.match(regex);
    var y = (m[1] ? parseInt(m[1]) : 0) * 1440 + (m[2] ? parseInt(m[2]) : 0) * 60 + (m[3] ? parseInt(m[3]) : 0);

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['mytime-desc']  = function(a,b) {
    // Same function as above, except for the return value:
    return ((x < y) ? -1 : ((x > y) ? -1 : 0));
}

And the associated jQuery dataTable call would look something like:

$('#example').dataTable( {
    "aoColumns": [
        null,
        null,
        null,
        { "sType": "mytime" },
        null
    ]
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜