I have the following table markup:
<table class="table listagem-dados filtering-col table-bordered table-striped responsive" cellspacing="0" width="100%" summary="Esta tabela exibe os perfis de usuários existentes e os relaciona com tipos de usuário, tipos de perfil e seus respectivos status.">
<thead>
<tr>
<th></th>
<th id="coluna1">Perfil</th>
<th id="coluna2">Usuário</th>
<th id="coluna3">Tipo Perfil</th>
<th id="coluna4">Status</th>
</tr>
<tr class="filterrow">
<th><i class="material-icons md"></i></th>
<th class="input-filter"><input type="text" class="form-control tchatchaca" placeholder="Buscar por Perfil"></th>
<th class="select-filter"><select class="form-control" placeholder="Buscar por Usuário">
<option value="">Selecione</option>
<option value="fornecedor">Fornecedor</option>
<option value="Governo">Governo</option>
<option value="Outros Entes">Outros Entes</option>
<option value="Terceirizados">Terceirizados</option>
</select></th>
<th class="select-filter"><select class="form-control" placeholder="Buscar por Tipo Perfil">
<option value="">Selecione</option>
<option value="Comum">Comum</option>
<option value="Especial">Especial</option>
<option value="Administrativo">Administrativo</option>
</select></th>
<th class="select-filter"><select class="form-control" placeholder="Buscar por Status">
<option value="">Selecione</option>
<option value="Bloqueado">Bloqueado</option>
<option value="Desbloqueado">Desbloqueado</option>
<option value="Excluido">Excluido</option>
</select></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="selectButton" value="Fornecedor"/>
<div class="btn-toolbar btn-micro" role="toolbar" aria-label="Ações">
<button type="button" class="btn btn-primary" title="Editar">
<i class="material-icons" aria-label="editar" ></i>
<span>Editar</span>
</button>
<button type="button" class="btn btn-default" title="Bloquear">
<i class="material-icons" aria-label="bloquear"></i>
<span>Desbloquear</span>
</button>
<button type="button" class="btn btn-danger" title="Excluir">
<i class="material-icons" aria-label="excluir"></i>
<span>Excluir</span>
</button>
</div>
</td>
<td id="linha1 fornecedor"><a href="#nogo">Fornecedor</a></td>
<td headers="linha1 coluna2">Fornecedor</td>
<td headers="linha1 coluna3">Comum</td>
<td headers="linha1 coluna4">Desbloqueado</td>
</tr>
<tr>
<td>
<input type="checkbox" name="selectButton" value="Fornecedor"/>
<div class="btn-toolbar btn-micro" role="toolbar" aria-label="Ações">
<button type="button" class="btn btn-primary" title="Editar">
<i class="material-icons" aria-label="editar" ></i>
<span>Editar</span>
</button>
<button type="button" class="btn btn-default" title="Bloquear">
<i class="material-icons" aria-label="bloquear"></i>
<span>Bloquear</span>
</button>
<button type="button" class="btn btn-danger" title="Excluir">
<i class="material-icons" aria-label="excluir"></i>
<span>Excluir</span>
</button>
</div>
</td>
<td id="linha2 sisAdm"><a href="#nogo">Administrador do Sistema</a></td>
<td headers="linha2 coluna2">Governo</td>
<td headers="linha2 coluna3">Especial</td>
<td headers="linha2 coluna4">Bloqueado</td>
</tr>
<tr>
<td>
<input type="checkbox" name="selectButton" value="Fornecedor"/>
<div class="btn-toolbar btn-micro" role="toolbar" aria-label="Ações">
<button type="button" class="btn btn-primary" title="Editar">
<i class="material-icons" aria-label="editar" ></i>
<span>Editar</span>
</button>
<button type="button" class="btn btn-default" title="Bloquear">
<i class="material-icons" aria-label="bloquear"></i>
<span>Bloquear</span>
</button>
<button type="button" class="btn btn-danger" title="Excluir">
<i class="material-icons" aria-label="excluir"></i>
<span>Excluir</span>
</button>
</div>
</td>
<td id="linha2 sisAdm"><a href="#nogo">Administrador do Sistema</a></td>
<td headers="linha2 coluna2">Governo</td>
<td headers="linha2 coluna3">Administrativo</td>
<td headers="linha2 coluna4">Bloqueado</td>
</tr>
</tbody>
</table>
As you can see, in this table a have 2 rows in header where the first is for the headings and sorting and the second is for filtering. Some of the filters will happen throug a <input type="text">
and others through a <select>
.
I initialized the Datatable with the following code.
$(document).ready(function() {
// DataTable
var table = $('table').DataTable({
"language": {
"lengthMenu": "Mostrar _MENU_ registros por página",
"zeroRecords": "Nenhum registro encontrado",
"info": "Showing page _PAGE_ of _PAGES_",
"infoEmpty": "No records available",
"infoFiltered": "(filtered from _MAX_ total records)"
},
});
// Apply the search
table.columns( '.input-filter' ).every( function () {
var that = this;
$( 'input', this.header() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
// Apply the search
table.columns( '.select-filter' ).every( function () {
var that = this;
$( 'select', this.header() ).on( 'change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
} else if ( that.search() !== "" ) {
that
.draw();
}
} );
} );
} );
But by default DataTables sets the sorting on the last/second row of header. Then i put the "bSortCellsTop": true
in the initialization:
// DataTable
var table = $('table').DataTable({
"language": {
"lengthMenu": "Mostrar _MENU_ registros por página",
"zeroRecords": "Nenhum registro encontrado",
"info": "Showing page _PAGE_ of _PAGES_",
"infoEmpty": "No records available",
"infoFiltered": "(filtered from _MAX_ total records)"
},
"bSortCellsTop": true
});
And the sorting is now on the first row as expected. But the searching is simply not working. I type in the input and the response is nothing. In de console log are no errors. I already tried everything I could. I Changed classes, disabled others dependencies, got the latest version of Datatable, changed location of tags and many others things.
CAUSE
It seems that with complex header, you cannot access header columns that are not used for sorting with columns()
.
SOLUTION
Rewrite your code as follows:
// Apply the search
$.each($('.input-filter', table.table().header()), function () {
var column = table.column($(this).index());
$( 'input', this).on( 'keyup change', function () {
if ( column.search() !== this.value ) {
column
.search( this.value )
.draw();
}
} );
} );
// Apply the search
$.each($('.select-filter', table.table().header()), function () {
var column = table.column($(this).index());
$( 'select', this).on( 'change', function () {
if ( column.search() !== this.value ) {
column
.search( this.value )
.draw();
} else if ( column.search() !== "" ) {
column
.draw();
}
} );
} );
DEMO
See this jsFiddle for code and demonstration.
NOTES
"orderCellsTop": true
instead of bSortCellsTop
for DataTables 1.10+If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With