简体   繁体   中英

DataTables custom detection plug-in and sorting order

i'm trying to implement a function on Datatables that has to look up the table data, do a regex and then, if it returns true, then, when i click on the header to sort data, it will sort it by the last 5 digits, ignoring the letters that comes up in the beginning of the string.

i have the following code

$.fn.dataTable.ext.oSort['custom'] = function (settings, col) {
    return this.api().column(col, {order: 'index'}).nodes().map(function (td, i) {
        var string= $(td).html();
        return $.trim(string.substr(string.length - 4));
    });
}


$.fn.dataTable.ext.type.detect.push(
    function (value) {
        var test = (/PT\d/).test(value);
        return test ? 'custom' : null;
    }
);

this is for a custom data that has lots of trash in the beggining, like country code and stuff, but the data order is only by the last 5 digits.

i've been searching all over i'm having a hard time to understand and debug. Debuguing the detect works, if 1 put an alert, it gives-me true when it hits the colum with the values i want, but the custom sorting doesn't work, can anybody help?

hope i'm clear about it

thanks

actualy i solved it myself.

the problem was that DataTables needs to make the entire column return true, so, if the regex fails in any value in the same column it fails.

$.fn.dataTable.ext.type.detect.unshift(
    function (d) {
        var pt = (/^PT\d/).test(d);
        var es= (/^ES\d/).test(d);
        var en= (/^EN\d/).test(d);
        if (pt || es|| en) {
            return 'custom'
        } else {
            return false;
        }
    }
);

$.fn.dataTable.ext.type.order['custom-pre'] = function (string) {
    return $.trim(string.substr(string.length - 4));
};

so this is my last code used and it works just fine.

i'm posting it so anybody with the same problem can have a clue to solve some future problem :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM