han*_*sen 13 javascript jquery non-ascii-characters accent-insensitive jquery-datatables
使用jQuery DataTables时,是否可以在使用过滤器时进行不区分重音的搜索?例如,当我输入'e'字符时,我想用'e'或'é','è'搜索每个单词.
我想到的是将字符串规范化并将它们放入一个单独的隐藏列中,但这并不能解决字母顺序问题.
我尝试了以下方法:
$.fn.dataTableExt.ofnSearch = 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;
};
Run Code Online (Sandbox Code Playgroud)
你在编辑中如此接近,你尝试了什么,除了你缺少必须重要的东西:定义你的过滤插件的"范围"或sType.永远不会调用该函数,对吧?这是因为插件与a没有关联sType.
要使代码工作,只需像这样声明插件:
$.fn.dataTableExt.ofnSearch['string'] = function ( data ) {
return ! data ?
'' :
typeof data === 'string' ?
..
..
}
Run Code Online (Sandbox Code Playgroud)
看到这个小提琴 - > http://jsfiddle.net/Y5ycM/我已经将一些"Internet Explorer"栏目变成了"Internetéxplorer",一些"Opera"变成了"öpera"等,尝试搜索ex或op.
请参阅https://datatables.net/development/filtering,"基于类型的列过滤"部分:
为列分配sType(或通过DataTables或类型检测插件自动检测到它)时,通常会将其用于自定义排序,但它也可用于提供自定义过滤选项.这是通过使用与该目标列的sType匹配的参数名称向对象添加函数来完成的:
它说,我猜这里的文档中存在拼写错误或疏忽
$.fn.dataTableExt.ofnSearch但应该正确地说$.fn.dataTableExt.ofnSearch[sType],正如后面的例子清楚地指出的那样.
更新.艾伦·怡和证实这是1.9.4的错误在这里:
是的 - 1.9.4中的错误.1.9.4遗憾地打破了这部分代码.它还会影响从过滤器中删除HTML标记,因为它使用相同的机制.它固定在1.10测试版中.
jQuery v:2.1.0
jquery.dataTables.min.js v:1.10.7
dataTables.responsive.min.js v:1.0.6
我在这里使用了重音中和插件(感谢Allan Jardine):http:
//www.datatables.net/plug-ins/filtering/type-based/accent-neutralise
并且只为我的目的做了最小的调整:
我结合了以下答案的例子:http://jsfiddle.net/xebtyaeL/5/
jQuery.fn.DataTable.ext.type.search.string = function ( 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;
};
var tabla = jQuery('#datatable-table').dataTable( { } );
new jQuery.fn.dataTable.Responsive( tabla );
jQuery(document).ready(function() {
var table = jQuery('#datatable-table').DataTable();
// Remove accented character from search input as well
jQuery('#datatable-table_filter input').keyup( function () {
table
.search(
jQuery.fn.DataTable.ext.type.search.string( this.value )
)
.draw();
} );
} );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9103 次 |
| 最近记录: |