使用自定义sTyle从过滤器中剥离HTML标记

时间:2013-06-06 19:29:32

标签: javascript jquery html datatables

我有一个定义为

的数据表
        $('#group').dataTable( {
         "sDom": '<"H"fi>t<"F">', 
        "aaSorting": [ [2,'asc'], [1,'asc'] ],
        "aoColumnDefs": [
            {"aTargets": [ 0 ],      "sType": null,         "bSortable": false, "bSearchable": false },       
            {"aTargets": [ 1, 2 ],   "sType": "html",       "asSorting": [ "asc", "desc" ] },      
            {"aTargets": [ 3 ],      "sType": "gcse-grade", "asSorting": [ "desc", "asc" ] },      
            {"aTargets": [ "_all" ], "sType": "grade",      "asSorting": [ "desc", "asc" ] } 
        ],
        "bAutoWidth": false,
        "bFilter": true,
        "bInfo": true,
        "bLengthChange": false,
        "bPaginate": false,
        "bScrollAutoCss": true,
        "bScrollCollapse": true,
        "bSort": true,
        "oLanguage": { "sSearch": "_INPUT_" }
     }
    );

如您所见,我使用了名为gradegcse_grade的自定义sTypes。我使用oSort进行自定义排序工作正常。但是,当我创建表时,这些列有时会在其中包含HTML标记。

如何过滤这些内容,以便首先从内部剥离HTML标记。即所以过滤器只看到文本,而不是标签(因为我不希望过滤器拿起任何标签或标签)。

我这里有一个fiddle

2 个答案:

答案 0 :(得分:2)

要在过滤器搜索上删除html标记,您可以使用以下内容:

"aoColumnDefs": [
    {   "aTargets": [0],
        "mRender": function ( data, type, full ) {
            if (type === 'filter') {
                return data.replace(/(<([^>]+)>)/ig,"");
            }

            return data;
        }
    }
]

答案 1 :(得分:0)

如果这仅用于分类目的,请尝试以下方法:

jQuery.fn.dataTableExt.oSort['gcse-grade-asc']  = function (a, b) {
    var x = $('<div />').append(a).text();      // append to div element and get [inner]text to strip tags
    var y = $('<div />').append(b).text();      // append to div element and get [inner]text to strip tags
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));  // sort like normal
};

jQuery.fn.dataTableExt.oSort['gcse-grade-desc'] = function (a, b) {
    var x = $('<div />').append(a).text();      // append to div element and get [inner]text to strip tags
    var y = $('<div />').append(b).text();      // append to div element and get [inner]text to strip tags
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));  // sort like normal
};

jQuery.fn.dataTableExt.oSort['grade-asc']  = function (a, b) {
    var x = $('<div />').append(a).text();      // append to div element and get [inner]text to strip tags
    var y = $('<div />').append(b).text();      // append to div element and get [inner]text to strip tags
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));  // sort like normal
};

jQuery.fn.dataTableExt.oSort['grade-desc'] = function (a, b) {
    var x = $('<div />').append(a).text();      // append to div element and get [inner]text to strip tags
    var y = $('<div />').append(b).text();      // append to div element and get [inner]text to strip tags
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));  // sort like normal
};

如果您需要进行过滤(以及排序等等),只需使用fnRowCallback函数来操作绘制表格之前的实际内容:

"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    var gcse = $('td:eq(2)', nRow),     // cache lookup
        grade = $('td:eq(3)', nRow);    // cache lookup
    gcse.html(gcse.text());
    grade.html(grade.text());
}