[英]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.