Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
Column order doesn't change in DataTables plugin

I wanted to reverse column on export data from DataTables. I look for method to do it and finally ended up with this way:

$(document).ready(function(){
            var arrayCol = new Array();
            var table = $('#example').DataTable({
                dom: 'Bfrtip',
                initComplete:function (  ) {
                    var len = this.api().columns().count();
                    var array =  Array.from(Array(len).keys())
                    arrayCol = array.reverse();
                  },
            buttons: [
                {
                    extend: 'excelHtml5',
                     exportOptions: {
                            columns: ':visible:not(.not-export-col)',
                             orthogonal: 'export'
                             
                        }
                },
               {
                   extend: 'pdfHtml5',
                   orientation: 'landscape',
                   pageSize: 'A4',
                    exportOptions: {
                
                    columns: arrayCol, // this doesn't work
                     //columns:[5,4,3,2,1,0], //this work
                        orthogonal: 'export'
                    
                   }
                    
                    
               }
    ]
                });
        });

The var arrayCol when debugging has values but when exporting to PDF the PDF doesn't have any columns.

Maybe it doesn't assign to columns or something like that.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the simplest way is to reverse each individual row array, as you are exporting the data. You can use exportOptions.rows to do this.

You also need to reverse the headers, which can be done using exportOptions.format.heeader. In this case, you only get access to one header field at a time, so there is a bit more work needed to build a reversed array of header values and then access each index location in that array:

$(document).ready(function() {

  var table = $('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
      { 
        extend: 'pdf',
        text: 'To PDF',
        exportOptions: {
          rows: function ( idx, data, node ) {
            return data.reverse();
          },
          format: {
            header: function ( data, idx, node ) {
              var headers = $('#example').DataTable().table().header();
              var reversedHeaders = headers.innerText.split('\t').reverse();
              return reversedHeaders[idx];
            }
          }
        }
      }
    ]
  } );

} );

References:

  • export options
  • row selectors - function
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda