简体   繁体   中英

Datatable doesn't respect screen width on resize

I have a datatable that doesn't respect the screen width when screen resize.

I tried putting width 100% but didn't work.

I tried creating a parent div with 100% width and didn't work either.

Tried autowidth. It didn't work.

'autowidth': false,

Tried table-layout:fixed but didnt worked

table{table-layout:fixed}

I use php to echo the answer of each row and then initiliaze the datatable.

<table id="produtos" class="hover display wrap dataTable no-footer dtr-inline collapsed" style="width: 100%;" role="grid" aria-describedby="produtos_info">
   <thead>
      <tr role="row">
         <th class="sorting_asc" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 53px;" aria-sort="ascending" aria-label="Pedido: Ordenar colunas de forma descendente">Pedido</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 59px;" aria-label="Cliente: Ordenar colunas de forma ascendente">Cliente</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 66px;" aria-label="Situação: Ordenar colunas de forma ascendente">Situação</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 86px;" aria-label="Pagamento: Ordenar colunas de forma ascendente">Pagamento</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 101px;" aria-label="Classif.: Ordenar colunas de forma ascendente">Classif.</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 66px;" aria-label="Emissão: Ordenar colunas de forma ascendente">Emissão</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 72px;" aria-label="Produção: Ordenar colunas de forma ascendente">Produção</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 67px;" aria-label="Faturam.: Ordenar colunas de forma ascendente">Faturam.</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 66px;" aria-label="Financ.: Ordenar colunas de forma ascendente">Financ.</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 66px;" aria-label="Exped.: Ordenar colunas de forma ascendente">Exped.</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 62px;" aria-label="Nº NF: Ordenar colunas de forma ascendente">Nº NF</th>
         <th class="sorting" tabindex="0" aria-controls="produtos" rowspan="1" colspan="1" style="width: 0px; display: none;" aria-label="Entrega: Ordenar colunas de forma ascendente">Entrega</th>
      </tr>
   </thead>
   <tbody>
      <tr role="row" class="odd">
         <td class="sorting_1 dtr-control">999999<input type="hidden" name="data[TESTE_C1]" value="08" id="TESTE_C1"></td>
         <td>NOME TESTE</td>
         <td>Faturado total</td>
         <td>Boleto</td>
         <td>TESTE</td>
         <td>21/01/2020</td>
         <td>/  /</td>
         <td>21/01/2020</td>
         <td>21/01/2020</td>
         <td>22/01/2020</td>
         <td>99999999</td>
         <td style="display: none;">sem dados</td>
      </tr>
      <tr role="row" class="even">
         <td class="sorting_1 dtr-control">999999<input type="hidden" name="data[TESTE_C1]" value="08" id="TESTE_C1"></td>
         <td>NOME TESTE</td>
         <td>Faturado total</td>
         <td>Boleto</td>
         <td>TESTE</td>
         <td>09/10/2020</td>
         <td>/  /</td>
         <td>09/10/2020</td>
         <td>09/10/2020</td>
         <td>09/10/2020</td>
         <td>99999999</td>
         <td style="display: none;">sem dados</td>
      </tr>
      <tr role="row" class="odd">
         <td class="sorting_1 dtr-control">A84505<input type="hidden" name="data[TESTE_C1]" value="08" id="TESTE_C1"></td>
         <td>NOME TESTE</td>
         <td>Faturado total</td>
         <td>Pago</td>
         <td>TESTE</td>
         <td>13/10/2020</td>
         <td>/  /</td>
         <td>14/10/2020</td>
         <td>14/10/2020</td>
         <td>19/10/2020</td>
         <td>99999999</td>
         <td style="display: none;">sem dados</td>
      </tr>
   </tbody>
</table>

Datatable configs

        var table = $('#produtos').DataTable({
            responsive: true,
            autowidth: false,
            stateSave: true,
            "language": {
                "url": "//cdn.datatables.net/plug-ins/1.10.20/i18n/Portuguese.json"
            },
            dom: 'Bfrtip',
            buttons: [
                //'copyHtml5',
                //'excelHtml5',
                //'csvHtml5',
                //'pdfHtml5'
            ],
        });

The datatable above is the code generated on the screen.

Imported files:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/jszip-2.5.0/dt-1.10.21/af-2.3.5/b-1.6.3/b-colvis-1.6.3/b-flash-1.6.3/b-html5-1.6.3/b-print-1.6.3/cr-1.5.2/fc-3.3.1/fh-3.1.7/kt-2.5.2/r-2.2.5/rg-1.1.2/rr-1.2.7/sc-2.0.2/sp-1.1.1/sl-1.3.1/datatables.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/jszip-2.5.0/dt-1.10.21/af-2.3.5/b-1.6.3/b-colvis-1.6.3/b-flash-1.6.3/b-html5-1.6.3/b-print-1.6.3/cr-1.5.2/fc-3.3.1/fh-3.1.7/kt-2.5.2/r-2.2.5/rg-1.1.2/rr-1.2.7/sc-2.0.2/sp-1.1.1/sl-1.3.1/datatables.min.js"></script>

I think it's not possible to do that, but you can make a responsive data table with responsive extension for datatables.

Add this lines in your code:

<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.7/css/responsive.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.min.css">

<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/responsive/2.2.6/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js"></script>

So, it seens to be a bug with datatable responsive plugin.

To solve this i had to change CSS with media queries for a few specific screens sizes.

I searched a lot and didnt find any answer that solve my problem.

When the screen re-sizes the datatable works fine, but when its a callback from ajax requistion, it doesnt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM