2015-11-05 405 views
2

我想在jQuery Datatables插件中搜索一些帶有特殊字符的單詞。jQuery的DataTables不工作的特殊字符導致搜索

有一些結果數據表是這樣的:

Peinado, Alma_María 
Aguilar Castillo, Antonio José 

當我嘗試搜索 「alma_maría」,第一個結果顯示:

Peinado, Alma_María 

當我嘗試 「alma_maria」(注我使用字符「我」而不是「í」),沒有任何顯示。

截圖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> 
+0

ü[R使用哪個數據庫? –

+0

你是什麼意思?數據是從JSON。 – joan16v

+0

你可以試試這個http://www.datatables.net/plug-ins/filtering/type-based/accent-neutralise –

回答

0

我不知道爲什麼,但 「口音中和」 是不是爲我工作。

我有一個像魅力一樣工作的創意。

我在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("&aacute;", "a", $String); 
    $String = str_replace("&Aacute;", "a", $String); 
    $String = str_replace("&eacute;", "e", $String); 
    $String = str_replace("&Eacute;", "e", $String); 
    $String = str_replace("&iacute;", "i", $String); 
    $String = str_replace("&Iacute;", "i", $String); 
    $String = str_replace("&oacute;", "o", $String); 
    $String = str_replace("&Oacute;", "o", $String); 
    $String = str_replace("&uacute;", "u", $String); 
    $String = str_replace("&Uacute;", "u", $String); 
    return $String; 
} 

,以取代其非特殊字符版本特殊字符。

然後添加到JSON中的「名稱」字段中,這個非特殊碼版本在HTML註釋中。

我:

$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); 
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; 
}; 

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

+0

我想是這樣的: http://pastebin.com/pLHGpW2W 但它不工作! – joan16v