4

I'm attempting to use a custom input for searching/filtering a jQuery Datatables table. I'm using jQUery 2.1.1 and DataTables 1.10.4. My code is pretty simple, and mirrors what's in the API docs:

var table = $('#pList').DataTable({  
    "pageLength": 25,
    "lengthChange": false,
    "ajax": 'partsJSON.cfm'

    });

$('#pdb-filter').on( 'keyup', function () {
    console.log(table);
    table.search( this.value ).draw();
} );

When I run the page and make entries in the input, I get the following in the console:

TypeError: table.search(...).draw is not a function 

Followed by:

Object { context: Array[1], selector: Object, tables: O/q.extend/h/<(), table: O/q.extend/h/<(), draw: O/q.extend/h/<(), page: O/q.extend/h/<(), ajax: Object, rows: O/q.extend/h/<(), row: O/q.extend/h/<(), columns: O/q.extend/h/<(), 14 more… }

My reading of that says that "draw" is the fifth element in the table object.

Additionally, when I type the code into the console manually, (without the init options, of course) everything works fine. I get no errors.

Anybody got any ideas?'

dbrosier
  • 330
  • 4
  • 11
  • 1
    If table.search( this.value ) returned no results, might the draw() function not be available? What is returned by table.search( this.value )? Is it the expected object? Does the debugger indicate a draw() function is available? – Greg Lafrance Nov 21 '14 at 18:11
  • 2
    Nice catch! I changed the line to "table.search( $('#pdb-search-term').val()).draw();" Post an answer: I'll rep you. – dbrosier Nov 21 '14 at 18:23

1 Answers1

0

If you are using datatable version 1.9 or lower then use

table.fnDraw()

from version 1.10 use

table.draw()

Avnish Tiwary
  • 2,188
  • 22
  • 27