我正在尝试在jQuery Datatables插件中搜索一些带有特殊字符的单词。
数据表中有一些结果如下:
Peinado, Alma_María
Aguilar Castillo, Antonio José
当我尝试搜索“alma_maría”时,会显示第一个结果:
Peinado, Alma_María
当我尝试“alma_maria”时(注意我使用的是字符“i”而不是“í”),没有显示任何内容。
屏幕截图1:
截屏2:
当我搜索没有特殊字符时,有没有办法配置数据表以显示特殊字符结果?
我的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>
答案 0 :(得分:0)
答案是口音中和。并且有适合它的插件。
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;
};
答案 1 :(得分:0)
我不知道为什么,但“Accent Neutralization”对我不起作用。
我的创意很有魅力。
我在PHP方面使用了这样的函数:
public function cleanString($String)
{
$String = str_replace(array('á','à','â','ã','ª','ä'), "a", $String);
$String = str_replace(array('Á','À','Â','Ã','Ä'), "a", $String);
$String = str_replace(array('Í','Ì','Î','Ï'), "i", $String);
$String = str_replace(array('í','ì','î','ï'), "i", $String);
$String = str_replace(array('é','è','ê','ë'), "e", $String);
$String = str_replace(array('É','È','Ê','Ë'), "e", $String);
$String = str_replace(array('ó','ò','ô','õ','ö','º'), "o", $String);
$String = str_replace(array('Ó','Ò','Ô','Õ','Ö'), "o", $String);
$String = str_replace(array('ú','ù','û','ü'), "u", $String);
$String = str_replace(array('Ú','Ù','Û','Ü'), "u", $String);
$String = str_replace(array('[','^','´','`','¨','~',']'), "", $String);
$String = str_replace("ç", "c", $String);
$String = str_replace("Ç", "C", $String);
$String = str_replace("ñ", "n", $String);
$String = str_replace("Ñ", "N", $String);
$String = str_replace("Ý", "Y", $String);
$String = str_replace("ý", "y", $String);
$String = str_replace("á", "a", $String);
$String = str_replace("Á", "a", $String);
$String = str_replace("é", "e", $String);
$String = str_replace("É", "e", $String);
$String = str_replace("í", "i", $String);
$String = str_replace("Í", "i", $String);
$String = str_replace("ó", "o", $String);
$String = str_replace("Ó", "o", $String);
$String = str_replace("ú", "u", $String);
$String = str_replace("Ú", "u", $String);
return $String;
}
用非特殊字符版本替换特殊字符。
然后在“名称”字段中将这个非特殊字版本添加到JSON中。
我有:
$arrayJson = array();
foreach ($arrayUsers as $item) {
$arrayJson[] = array(
$item['name'],
$item['city'],
$item['email'],
$item['phone'],
$item['id'],
$item['colleged'],
$item['asesor']
);
}
$jsonStr = json_encode($arrayJson);
现在我有:
$arrayJson = array();
foreach ($arrayUsers as $item) {
$cleanName = $Utils->cleanString($item['name']);
$arrayJson[] = array(
'<!-- ' . $cleanName . ' -->' . $item['name'],
$item['city'],
$item['email'],
$item['phone'],
$item['id'],
$item['colleged'],
$item['asesor']
);
}
$jsonStr = json_encode($arrayJson);