Complex sorting and searching problem with complex title

I have the following table layout:

<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">&#xE8B6;</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" >&#xE254;</i>
        <span>Editar</span>
       </button>
       <button type="button" class="btn btn-default" title="Bloquear">
        <i class="material-icons" aria-label="bloquear">&#xE898;</i>
        <span>Desbloquear</span>
       </button>
       <button type="button" class="btn btn-danger" title="Excluir">
        <i class="material-icons" aria-label="excluir">&#xE872;</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" >&#xE254;</i>
        <span>Editar</span>
       </button>
       <button type="button" class="btn btn-default" title="Bloquear">
        <i class="material-icons" aria-label="bloquear">&#xE899;</i>
        <span>Bloquear</span>
       </button>
       <button type="button" class="btn btn-danger" title="Excluir">
        <i class="material-icons" aria-label="excluir">&#xE872;</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" >&#xE254;</i>
        <span>Editar</span>
       </button>
       <button type="button" class="btn btn-default" title="Bloquear">
        <i class="material-icons" aria-label="bloquear">&#xE899;</i>
        <span>Bloquear</span>
       </button>
       <button type="button" class="btn btn-danger" title="Excluir">
        <i class="material-icons" aria-label="excluir">&#xE872;</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 there are 2 rows in the header, where the first is for headers and sorting, and the second is for filtering. Some of the filters will pass through <input type="text">, while others will pass through <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 will sort by the last / second line of the header. Then I put "bSortCellsTop": truein 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
});

, . . . . , . , , Datatable, .

+4
1

, , columns().

:

// 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

jsFiddle .

  • "orderCellsTop": true bSortCellsTop DataTables 1.10 +
  • "Bloqueado" "Desbloqueado".
+3

Source: https://habr.com/ru/post/1606967/


All Articles