dtlvd dtlvd - 1 year ago 488
jQuery Question

Datatable draw() method not working on column filter

After spending some days trying many solutions I've found on the Internet I'm
asking here.

I have form which display a table containing datas when search button is clicked. The table has 8 columns and on 3 of them I want to add a text input with which a filter with the column datas is applied. For a better understanding of my need there is the JsFiddle showing a working column filter.

So, I tried the solution of the link above and of the Datatable exemple without success and can't find what I'm doing wrong.

There is my code :

<table id="EquipmentTable" class="table table-striped table-bordered bottom-buffer" width="100%">
<th><input type="checkbox" name="select_all" value="1" id="checkAll" class="text-center" onclick="$.fn.backboneSearch.checkAllResult()"></th>
<th>Famille d'équipement</th>
<th>Gamme d'équipement</th>
<th>Site de stockage</th>
<th>Salle technique</th>
<tfoot id="backboneSearchtfoot">
<tr id="filterrow">
<th id="textFilter1" class="textFilter"></th>
<th id="textFilter2" class="textFilter"></th>
<th id="textFilter3" class="textFilter"></th>
<th class="listFilter"></th>

// Setup - add a text input to each footer cell
$('#EquipmentTable tfoot th.textFilter').each(function (i) {
$(this).html('<input type="text" data-index="' + i + '" />');

equipmentTable = $('#EquipmentTable').DataTable({
aaData: result,
aoColumns: [
{ mData: 'Identifier' },
{ mData: 'Mnemo' },
{ mData: 'FamGam.Family' },
{ mData: 'FamGam.Gamme' },
{ mData: 'dataState.Libelle' },
{ mData: 'IdentifierUI' },
{ mData: 'TechnicalRoom.InterventionUnitySenderSite' },
{ mData: 'IdentifierTechnicalRoom' },
bDestroy: true,
bFilter: false,
bRetrieve: true,
buttons: [{
className: 'btn-warning',
columns: [1, 2, 3, 4, 5, 6],
extend: 'excel',
fieldSeparator: ';',
text: '<span class="glyphicon glyphicon-export"></span> Export'
dom: 'Bfrtip',
language: { /*not useful to show*/ },
stateSave: true,
bProcessing: true

$(equipmentTable.table().container()).on('keyup', 'tfoot th.textFilter input', function () {

used by
is a json I get on the ajax success of the search Rest method. I populate the table on that success method.

So my question is : what have I done wrong or misunderstood ?
I've tried to compare the object
with what is returned on the exemple and getting equivalent object. That's why I'm almost sure that the problem is coming from the draw() method.

Thank you for your help.

Answer Source

Here is the working fiddle

First, your search is not working because you set bFilter to false. Then just remove this line or set this param to true :

bFilter: true,

But is not enough. Your loop used to draw input text column will not working because the column index begin at 0. Then if you set the first column at the second column, and you search on your first input, the sort will be done on the column 0. Then I added +1 to your data-index :

$(equipmentTable.table().container()).on('keyup', 'tfoot tr th.textFilter input', function () {
    equipmentTable.column($(this).data('index') + 1)

Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download