Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.fn.dataTable.ext.search.push not getting called

I am trying to use data table search plugin for filtering out my HTML table. I am getting faced with following problem.

I have a program as following, But when i check whether the jquery function $.fn.dataTable.ext.search.push works or not it results, it doesnt.

<div class="page-wrapper">
<nav aria-label="breadcrumb">

   <div class="breadcrumb">


        <div class="pull-right-button">
            <button type="button" onclick="exportTableToExcel('example')" class="btn btn-success"><i class="fa fa-file-excel-o" aria-hidden="true"></i> Download Report</button>
            <button type="button" onclick="showFilters()" class="btn btn-success"> Filters</button>
        </div>
     </div>
</nav>
  <div class="container-fluid" >
     <div class="col-sm-12">
        <div class="card">

          <table border="0"  cellpadding="5">
        <tbody><tr>
            <td>Minimum age:</td>
            <td><input type="text" id="min" name="min"></td>
            <td>Maximum age:</td>
            <td><input type="text" id="max" name="max"></td>
        </tr>
    </tbody></table>

          <div class="table-responsive" >

  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <div>

          <table id="example" class="display" style="width:100%">
        <thead>

          //some data

        </thead>
        <tbody>

            //some data

        </tbody>
    </table>


        </div>
    <div class="pagination">
  <?= $this->pagination->create_links();?>
</div>


         </div>
    </div>
</div> 
<script type="text/javascript">
    function exportTableToExcel(tableID, filename = ''){
        \\export function
    }
</script>
<script >

      $('#example').dataTable({
        searching: false,
    "paging": false, info: false
});


      /* Custom filtering function which will search data in column four between two values */
$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
      // var Row = document.getElementById("data_row");
      // var Cells = Row.getElementsByTagName("td");
      //   var age = Cells[0].innerText;
      //   console.log(age);
      //   var min = document.getElementById("min").value;
      //   var max = document.getElementById("max").value;// use data for the age column

      var min = parseInt( $('#min').val(), 10 );
        var max = parseInt( $('#max').val(), 10 );
        var age = parseFloat( data[3] ) || 0;

        if ( ( isNaN( min ) && isNaN( max ) ) ||
             ( isNaN( min ) && age <= max ) ||
             ( min <= age   && isNaN( max ) ) ||
             ( min <= age   && age <= max ) )
        {
            return true;
        }
        return false;
    }
);

$(document).ready(function() {
    var table = $('#example').DataTable();

    // Event listener to the two range filtering inputs to redraw on input
    $('#min, #max').keyup( function() {
        table.draw();
    } );
} );


    </script>

I referred and borrowed the source code from following sources https://datatables.net/examples/plug-ins/range_filtering.html also referrd following stackoverflow sources, jQuery DataTable filtering - so confusing

But i am not able to get a satiscatory solution. It would be great if someone could tell me what am i doing wrong?

Thank you for your suggestions

like image 476
mach2 Avatar asked Aug 30 '18 12:08

mach2


2 Answers

Couple of problem with your code.

  1. You're initializing DataTables multiple times, an old way .dataTable() and a new way .DataTable() You should use new way (1.10+)
  2. The column being fileted in this function $.fn.dataTable.ext.search.push follows 0 based index, so if that's 4th column if you count from 1, you need to use data[3]. Change your code accordingly.

Here is the working fiddle.

like image 80
jeetaz Avatar answered Oct 21 '22 13:10

jeetaz


Anybody having this issue should enable searching in datatable

 $('#example').dataTable({
    searching: true,
"paging": false, info: false

});

like image 41
makwana.a Avatar answered Oct 21 '22 12:10

makwana.a