首页 > 解决方案 > 当列内容为html时,jQuery Datatables过滤列

问题描述

我在要过滤的数据表中有一个列。我可以按如下定义的列进行过滤:

"columns": [
    { 
        "data": function (data) {
             return data['columnToFilter'];
        }
    },
];

但是,我找不到一种方法来过滤超过原始值的列,例如这个:

"columns": [
    { 
        "data": function (data) {
            return '<h1>' + data['columnToFilter'] + '</h1>';
         }          
    }
];

如何告诉 dataTables 第二列的原始值(没有 html)是什么?

这是我进行过滤的代码:

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        // tabLocation can only be filtered if the value is returned without additional html
        var tabLocation = data[0];
        if ($("#myFilter").hasClass("down")) {
            return true;
        } else {
            if (tabLocation == 0) {
                return true;
            }
        }
    }
);

$.fn.toggleText = function(t1, t2){
    if (this.text() == t1) this.text(t2);
    else                   this.text(t1);
    return this;
};

标签: jquerydatatables

解决方案


我使用的解决方案是这样的:

"columns": [
    { 
        "data": function (data) {
            return data['columnToFilter'];
        },
        "render": {
            "display": function ( data, type, row, meta ) {
                return '<h1>' + data['columnToFilter'] + '</h1>';
            },
        },
        "bSortable": false,
    },
];

推荐阅读