0

我有一个定义为的数据表

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

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

我如何过滤这些,以便它首先从内部剥离 HTML 标签。也就是说,过滤器只看到文本,而不是标签(因为我不希望过滤器拾取任何 , 或标签)。

我这里有一个小提琴

4

2 回答 2

2

要在过滤器搜索中去除 html 标签,您可以使用以下内容:

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

            return data;
        }
    }
]
于 2014-05-26T03:21:19.140 回答
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());
}
于 2013-06-06T23:10:01.527 回答