使用自定义sType从filter中剥离HTML标记
我有一个定义为的数据表
$('#group').dataTable( { "sDom": 't', "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_" } } );
如您所见,我使用了名为grade
和gcse_grade
自定义gcse_grade
。 我使用oSort进行自定义排序工作正常。 但是,当我创建表时,这些列有时会在其中包含HTML标记。
如何过滤这些内容,以便首先从内部剥离HTML标记。 即所以filter只看到文本,而不是标签(因为我不希望filter拿起任何标签或标签)。
我这里有个小提琴
要在filter搜索上删除html标记,您可以使用以下内容:
"aoColumnDefs": [ { "aTargets": [0], "mRender": function ( data, type, full ) { if (type === 'filter') { return data.replace(/(<([^>]+)>)/ig,""); } return data; } } ]
如果这仅用于排序目的,请尝试以下操作:
jQuery.fn.dataTableExt.oSort['gcse-grade-asc'] = function (a, b) { var x = $('').append(a).text(); // append to div element and get [inner]text to strip tags var y = $('').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 = $('').append(a).text(); // append to div element and get [inner]text to strip tags var y = $('').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 = $('').append(a).text(); // append to div element and get [inner]text to strip tags var y = $('').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 = $('').append(a).text(); // append to div element and get [inner]text to strip tags var y = $('').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()); }