0

JQuery 的DataTables的文档很差。很抱歉可能是微不足道的问题。

我想通过某些列上的某些字符串过滤我的表。这是我的代码:

var str = "days";
my_table.fnFilter(str, 9);

我希望在 9 列(从 0 开始计数)中只有包含“days”字符串的行。但上面的代码并没有给我带来结果

我曾尝试将此代码与 RexEx 一起使用:

var regEx = "days"; 
my_table.fnFilter(regEx, 9, true);

这段代码:

var regEx = "^days$";
my_table.fnFilter(regEx, 9, true);

这个代码:

var regEx = ".*days.*";
my_table.fnFilter(regEx, 9, true);

一切都没有运气。我确定我的表在第 9 列有“101 天”字符串。我究竟做错了什么?

更新:我的专栏有自定义类型:

jQuery.fn.dataTableExt.oSort['days-asc'] = function (a, b) {
    var x = parseInt((trim(a) == "") ? 0 : trim(a.replace(/days/, "")));
    var y = parseInt((trim(b) == "") ? 0 : trim(b.replace(/days/, "")));
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['days-desc'] = function (a, b) {
    var x = parseInt((trim(a) == "") ? 0 : trim(a.replace(/days/, "")));
    var y = parseInt((trim(b) == "") ? 0 : trim(b.replace(/days/, "")));
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};

更新 2: 我的表初始化选项

 $.my_table = $("#myTable").dataTable({
    "iDisplayLength": 10,
    "bStateSave": false,
    "bFilter": true,
    "bSort": true,
    "bAutoWidth": false,
    "oLanguage": {
        "sLengthMenu": sStdMenu + " per page"
    },
    "sPaginationType": "full_numbers",
    "sDom": 'rti<"bottom2"p><"bottom1"l',
    "aaSorting": [
        [0,'asc']
    ],
    "aoColumns": [
        { "sType": "html-trimmed" },
        { "sType": "date-direct" },
        { "sType": "date-direct" },
        { "sType": "html" },
        { "sType": "percent" },
        { "sType": "money" },
        { "sType": "money" },
        { "sType": "money" },
        { "sType": "days" },            
        { "sType": "days" },
        { "sType": "days" },
        { "sType": "days" },
        { "sType": "days" },
        { "sType": "days" },
        { "sType": "html" },
        { "sType": "html" },
        null,
        null,
        null
    ]

});
4

1 回答 1

0

我看不出您的初始化代码或fnFilter函数的语法有什么问题。如果有帮助,这里有一个完整的fnFilter函数示例,所有参数都设置为默认值(filterString和除外column):

$(document).ready(function () {
    'use strict';
    var my_table = $('#example').dataTable(),
        filterDataTable = function filterDataTable() {
            var filterString = 'days',
                column = 9,
                treatAsRegex = false,
                smartFiltering = true,
                showFilter = true,
                caseInsensitive = true;
            my_table.fnFilter(filterString, column, treatAsRegex, smartFiltering, showFilter, caseInsensitive);
        };
    $('#someElement').click(function (e) {
        filterDataTable();
        e.preventDefault();
        return false;
    });
});

此外,在完全不相关的注释中,您的排序功能可以简化为:

jQuery.fn.dataTableExt.oSort['days-asc'] = function (a, b) {
    var x = parseInt(a, 10) || 0,
        y = parseInt(b, 10) || 0;
    return x - y;
};
jQuery.fn.dataTableExt.oSort['days-desc'] = function (a, b) {
    var x = parseInt(a, 10) || 0,
        y = parseInt(b, 10) || 0;
    return y - x;
};
于 2012-11-19T13:32:50.450 回答