Strip HTML tags from filter with custom sType

2019-06-11 08:48发布

I have a datatable which is defined as

        $('#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_" }
     }
    );

As you can see I have used custom sTypes called grade and gcse_grade. I have custom sorting working fine using oSort. However, when I create the table these columns sometimes have HTML tags within them.

How can I filter these so that it firstly strips the HTML tags from within. i.e. so the filter only sees the text, not the tags (as I don't want the filter to pick up any , or tags).

I have a fiddle here

2条回答
仙女界的扛把子
2楼-- · 2019-06-11 09:05

If this is just for sorting purposes, try this:

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
};

If you need it for filtering (and sorting, et. al.), just use the fnRowCallback function instead to manipulate the actual content before the table is drawn:

"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());
}
查看更多
Bombasti
3楼-- · 2019-06-11 09:08

To strip html tags on filter search, you can use something like this:

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

            return data;
        }
    }
]
查看更多
登录 后发表回答