简体   繁体   English

DataTables自定义检测插件和排序顺序

[英]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. 我正在尝试在Datatables上实现一个功能,该功能必须查找表数据,执行正则表达式,然后,如果返回true,则当我单击标题对数据进行排序时,它将按最后5个对它进行排序数字,忽略字符串开头出现的字母。

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. 这是针对一个自定义数据,该数据在开始时会有很多垃圾,例如国家/地区代码和内容,但数据顺序仅由后5位组成。

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? 调试检测工作,如果1发出警报,当它以我想要的值到达列时,它会给出真,但自定义排序不起作用,有人可以帮忙吗?

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. 问题在于,DataTables需要使整个列返回true,因此,如果正则表达式在同一列中的任何值中失败,则它将失败。

$.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 :) 我正在发布它,因此任何有相同问题的人都可以找到解决未来问题的线索:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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