javascript - jQuery DataTables 无法在搜索中使用特殊字符

标签 javascript jquery datatables accent-insensitive

我正在尝试在 jQuery Datatables 插件中搜索一些带有特殊字符的单词。

数据表中有一些结果如下:

Peinado, Alma_María
Aguilar Castillo, Antonio José

当我尝试搜索“alma_maría”时,显示第一个结果:

Peinado, Alma_María

当我尝试“alma_maria”(请注意,我使用字符“i”而不是“í”)时,没有显示任何内容。

屏幕截图1:

Trying to search "alma_maría"

屏幕截图2:

Trying to search "alma_maria"

有什么方法可以将数据表配置为在我搜索没有特殊字符的情况下显示特殊字符结果吗?

我的 HTML/Javascript 代码:

<table class="display table table-bordered table-striped" id="table-colegiados">
<thead>
<tr>
    <th>{$TXT.nombre}</th>
    <th>{$TXT.ciudad}</th>
    <th>{$TXT.email}</th>
    <th>{$TXT.telefono}</th>
    <th>{$TXT.dni}</th>
    <th>{$TXT.colegiado}</th>
    <th>{$TXT.asesor}</th>
    <th>{$TXT.editar}</th>
    <th>{$TXT.borrar}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<script type="text/javascript">
    $.fn.dataTableExt.oApi.fnGetFilteredNodes = function ( oSettings )
    {
        var anRows = [];
        for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
            var nRow = oSettings.aoData[ oSettings.aiDisplay[i] ].nTr;
            anRows.push( nRow );
        }
        return anRows;
    };
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
        if ( oSettings.nTable == document.getElementById( 'table-colegiados' )){
            var asesor = aData[6];
            var checked = $('#checkbox_asesores').is(':checked');
            if (checked) {
                if (asesor == '1') {
                    return true;
                }
            } else {
                return true;
            }
            return false;
        } else {
            return true;
        }
    });
    var oTable = $('#table-colegiados').dataTable({
            "aaSorting": [ [0,'asc'] ],
            'iDisplayLength': 25,
            "bProcessing": true,
            "bServerSide": false,
            "bDeferRender": true,
            "sAjaxSource": "ajax/getColegiados.php",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                $.getJSON( sSource, aoData, function (json) {
                    fnCallback(json)
                } );
            }
    });
    $('#checkbox_asesores').on("click", function(e) {
        oTable.fnDraw();
    });
</script>

最佳答案

答案是口音中和。并且有现成的插件。

http://www.datatables.net/plug-ins/filtering/type-based/accent-neutralise

$.fn.dataTableExt.ofnSearch['string'] = function ( data ) {
return ! data ?
    '' :
    typeof data === 'string' ?
        data
            .replace( /\n/g, ' ' )
            .replace( /á/g, 'a' )
            .replace( /é/g, 'e' )
            .replace( /í/g, 'i' )
            .replace( /ó/g, 'o' )
            .replace( /ú/g, 'u' )
            .replace( /ê/g, 'e' )
            .replace( /î/g, 'i' )
            .replace( /ô/g, 'o' )
            .replace( /è/g, 'e' )
            .replace( /ï/g, 'i' )
            .replace( /ü/g, 'u' )
            .replace( /ç/g, 'c' ) :
        data;
};

演示:http://jsfiddle.net/kishoresahas/416mvzws/

关于javascript - jQuery DataTables 无法在搜索中使用特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549484/

相关文章:

javascript - 如何在 Angular Directive(指令)中将值传递给模板?

jQuery fadeIn 和 fadeOut 在 Internet Explorer 6、7 或 8 中不起作用?

javascript - jQuery 的 Google.load() - 不适用于 DataTables.net

javascript - 如何对 jQuery DataTables 中的数字列进行排序

javascript - css 与返回 false 值的函数绑定(bind)

javascript - 如何在另一个函数完成后执行一个函数?

javascript - 当最后一个点在当前 X 轴最小值之前并且下一个点在当前 X 轴最大值之后时,highcharts 不显示线条

javascript - 将第一个元素聚焦在新行上

php - 在数据表 php 中获取复选框字段时出现问题

javascript - 如何限制在html输入文本字段中粘贴文本