繁体   English   中英

过滤带有或不带有重音的数据表

[英]Filter data with and without accents datatable jquery

我需要输入带重音的文字。 我会过滤匹配的行,包括带有重音的单词和没有重音的单词。

这是我的小提琴 显然,它适用于jquery.datatable的版本。

有人能帮我吗? 我希望它像这样出来:

$(document).ready(function() {
   $('#datatable-table').DataTable();
});

我有这张桌子

在此处输入图片说明

当我按arbol筛选时,仅显示我一行,但我显示两行,即“ arbol”和“árbol”(带有重音符号)。


编辑

我已经添加了以下代码,但是请注意,它不适用于具有空字段的列:

jQuery.fn.DataTable.ext.type.search.string = function ( data ) {
console.log('dataaaa: ' + data);
return ! data ?
    '' :
    typeof data === 'string' ?
        data
            .replace( /έ/g, 'ε')
            .replace( /ύ/g, 'υ')
            .replace( /ό/g, 'ο')
            .replace( /ώ/g, 'ω')
            .replace( /ά/g, 'α')
            .replace( /ί/g, 'ι')
            .replace( /ή/g, 'η')
            .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, 'a' )
            .replace( /õ/g, 'o' )
            .replace( /ç/g, 'c' )
            .replace( /ì/g, 'i' ) :
        data;
};

查看他们的API: https : //datatables.net/manual/api 也许提到是否支持自定义过滤器。 如果没有,您也可以通过更改此功能->

var table = $('#example').DataTable();

table.columns().flatten().each( function ( colIdx ) {
// Create the select list and search operation
var select = $('<select />')
    .appendTo(
        table.column(colIdx).footer()
    )
    .on( 'change', function () {
        table
            .column( colIdx )
            .search( $(this).val() ) <-- here
            .draw();
    } );

// Get the search data for the first column and add to the select list
table
    .column( colIdx )
    .cache( 'search' )
    .sort()
    .unique()
    .each( function ( d ) {
        select.append( $('<option value="'+d+'">'+d+'</option>') );
    } );

});

$('#datatable-table').DataTable({
   search: { regex: true }
});

let oldInput = $('.dataTables_filter input');
let newInput = $('<input>').on('change keyup input', () => {
  let regex = textToRegex(newInput.val());
  oldInput.val(regex).trigger('input');
});
oldInput.hide().after(newInput);

function textToRegex(value) {
  return value
    .toLowerCase()
    .split('')
    .map(c => {
      if (/[-[\]{}()*+?.,\\^$|#]/.test(c)) {
        return '\\' + c;
      }
      [
        /[aàáâãäå]/, /[oòóôõöø]/, /[eèéêë]/, /[cç]/, /[dð]/,
        /[ii̇ìíîï]/, /[uùúûü]/, /[nñ]/, /[sš]/, /[yÿý]/, /[zž]/
      ].some(r => {
        if (r.test(c)) {
          c = r.source;
          return true;
        }
      });
      return c;
    })
    .join('');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM