Dropdown Filter Problem Data Table (renitilize the datatable)

Dropdown Filter Problem Data Table (renitilize the datatable)

newhcknewhck Posts: 2Questions: 1Answers: 0

Hi everyone, im making a dropdown box that when you select the option search in the search box and filter the table, i know the error that is happening but i dont know how to fix it, the error is in this line..

var table = $('#example').DataTable({
   dom: 'lrtip'
});

$('#table-filter').on('change', function(){
   table.search(this.value).draw();   
});

i dont know how to call that without renitilize the datatable.

Here is my code, can some one help me please :tired_face:

$(document).ready(function() {
$('#example').DataTable( {
"order": [[ 2, 'desc' ]],
columnDefs: [
{ orderable: false, targets: 0 }, { orderable: false, targets: 4 }, { orderable: false, targets: 5 }
],
responsive: true,
language: {
        url: '/js/Spanish.json',
    },

"footerCallback":  function (row, data, start, end, display) {
  var api = this.api(), data;
  // Remove the formatting to get integer data for summation
  var intVal = function (i) {
    console.log(i)
    return typeof i === 'string' ?
      i.replace(/[\$,]/g, '') * 1 :
    typeof i === 'number' ?
      i : 0;
  };

        // Total over all pages
  totalsum = api
            .cells( null, 1, { page: 'current'} )
            .render('display')
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

  $(api.column(1).footer()).html(
    '' + totalsum + ' <img style="width: 15px; height: auto; position: relative; margin-top: -4px;" src="/img/slp.png">'
  );
    }
} );

var table = $('#example').DataTable({
   dom: 'lrtip'
});

$('#table-filter').on('change', function(){
   table.search(this.value).draw();   
});

} );

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    One problem is you are initializing Datatables twice. Move the dom option in line 36 of the second snippet into the other Datatables init options. Remove lines 37-39 and use var table = $('#example').DataTable( { on line 2. If this doens't help please provide a test case so we can take a look at your running code.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • newhcknewhck Posts: 2Questions: 1Answers: 0

    OMG!! Thas was the problem! now is working!! I swear I had tried that, something must have gone. Thanks Kevin!

Sign In or Register to comment.