David
David

Reputation: 35

How to jquery search in database after accent neutralise in datatables - not working

Hello I have table with datatables jquery plugin. I added script in index.php in footer (pages are loaded using switch) for accent neutralising, because I want to search without diacritics. This script is not working in fields from DB, but when I added to echo new row - so in this row everything is OK.

Here is part of my code from body:

  $stmt = sqlsrv_query( $conn, $sql );
  if( $stmt === false) {
      die( print_r( sqlsrv_errors(), true) );
  }
  echo "<div class='container'>

        <h5>Heading</h5>


      <table id='tabulka_kariet' class='table table-bordered'>
        <thead>
          <tr>
            <th>Kód karty</th>
            <th>Názov karty</th>
            <th>Špec 1 </th>
            <th>Špec 2 </th>
            <th>Špec 3 </th>
            <th>Špec 4 </th>


          </tr>
        </thead>

        <tfoot>
          <tr>
            <th>Kód karty</th>
            <th>Názov karty</th>
            <th>Špec 1 </th>
            <th>Špec 2 </th>
            <th>Špec 3 </th>
            <th>Špec 4 </th>

          </tr>
        </tfoot>
      <tbody>
                  <tr>
              <td>mála</td>
              <td>očivá</td>
              <td>lólo</td>
              <td>šesť</td>
              <td>nedeľa</td>
              <td>ľščťžýáíé</td>
            </tr>";


   while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo "<tr>";

  echo "<td>" . $row['Code'] . "</td>";
  echo "<td><a href=\"detail.php?id=" . $row['Id'] . " \">" . $row['Name'] . "</td>";
  echo "<td>" . $row['Specification'] . "</td>";
  echo "<td>" . $row['Specification2'] . "</td>";
  echo "<td>" . $row['Specification3'] . "</td>";
  echo "<td>" . $row['Specification4'] . "</td>";

  echo "</tr>";
  }
  echo "</tbody></table>";


   sqlsrv_free_stmt( $stmt);

  ?>

Here is script from footer:

      <script>
      $(document).ready( function () {
      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, 'l')
                  .replace( /[šŠ]/g, 's')
                  .replace( /[čČçÇ]/g, 'c')
                  .replace( /[řŘ]/g, 'r')
                  .replace( /[ťŤ]/g, 't')
                  .replace( /[žŽ]/g, 'z')
                  .replace( /[ýÝ]/g, 'y')
                  .replace( /[ďĎ]/g, 'd')
                  .replace( /[ňŇ]/g, 'n') :

              data;
  };
    var table = $('#tabulka_kariet').DataTable();

  } );
  </script>

2.problem is, that This script wont function when are empty fields with spaces. So in that columns this searching is not function. Maybe there will be problem and so question is, how to prevent that from DB...? Using if condition for every field? More about this problem here

My similar question few days ago Thanks for your answers :)

Upvotes: 1

Views: 623

Answers (1)

I answered the other question, but I write my answer here too.

To solve a problem of accents neutralizes plugin I add the same method that you add to jQuery.fn.DataTable.ext.type.search.string to the jQuery.fn.DataTable.ext.type.search.html.

You can see here: https://jsfiddle.net/ghsmaniotto/24n05tas/

I do it according to https://github.com/DataTables/Plugins/blob/master/filtering/type-based/accent-neutralise.js

Upvotes: 1

Related Questions