Get Row fields as JSON array only from column 0 and 6

Get Row fields as JSON array only from column 0 and 6

suporte@jung.com.brsuporte@jung.com.br Posts: 4Questions: 2Answers: 0
edited June 2017 in Free community support

I'm using datatables and PHP to make a list of purchase orders.
My table has 06 columns and, the column position number 0 and 6 are "invisible" and i want only these columns in my array and pass the array to another PHP page tha will process each purchase order numbers and send emails from these array data.

I'm facing a lot of problems to do that, i've opened another question with an similar issues but doesn't solve my problem (https://datatables.net/forums/discussion/43159/get-row-data-as-array-with-column-reader-as-field-name?new=1)

So, let's show my files:

My DataTables config file:

$(document).ready(function() {

  var table = $('table.OCS  ').DataTable({
    // ocultar coluna de emails

    columnDefs: [ {
            targets: [ 0,6 ],
             "visible": false,

        }
    ],

    // ordernar a coluna com indice 0 em ordem decrescente
    order: [[ 0, "asc" ]],

    //adicionar suporte para nagegação pelo teclado ao clicar em uma célula
    keys: true,

    // habilitar reorganização de colunas com drag'n drop
    colReorder: true,

    // habilitar reorganização de linhas com drag'n drop
    //rowReorder: true,

    // tabela responsiva
    //responsive: true

    fixedHeader: true,


    // Desabilitar paginação dos resultados
    paging:false,

    //define inicializador padrão como referencia para plugins
    dom: 'Bfrtip',

    // habilita botões para exportar a tabela
    buttons: [
      {
          // tipo para exportar
          extend: 'csv',

          // texto do botao
          text:   'Exportar para CSV',

          // nome do arquivo
          title: 'detalhamento_indicadores_ano_x_mes',
          exportOptions: {
                columns: ':visible'
            },

      },

      {
        extend: 'pdf',
        text: 'Exportar para PDF',
        title: 'detalhamento_indicadores_ano_x_mes',
        exportOptions: {
              columns: ':visible'
          },
      },

      {
        extend: 'excel',
        text: 'Exportar para Excel',
        title: 'detalhamento_indicadores_ano_x_mes',
        exportOptions: {
              columns: ':visible'
          },
      },

      {
        extend: 'copy',
        text: 'Copiar dados',
        title: 'detalhamento_indicadores_ano_x_mes',
        exportOptions: {
              columns: ':visible'
          },
      },

      {
        extend: 'print',
        exportOptions: {
              columns: ':visible'
          },
      },

      {
        extend: 'colvis',
        text:  'Colunas Visíveis',
        exportOptions: {
              columns: ':visible'
          },
      }

    ],


  });

  $('#example tbody').on( 'click', 'tr', function () {
    $(this).toggleClass('selected');
  } );

  $("#enviar").click(function(){
    // var data = table.rows(['.selected']).data().toArray();
    // var json = JSON.stringify( data );
    //
    // //alert(json);
    // var data2 = table.rows(['.selected']).data();
    // var plainArray = table
    // .column( 0 )
    // .data()
    // .toArray();
    //
    // //alert(plainArray);
    // alert(data);


    var rowData = table.rows(".selected").data().toArray();
    if(rowData.length > 0){
        var remapped = [];
        $.each(rowData, function(i, item){
            var a = {};
            $.each(myColumns, function(i, col){
                a[col.title] = item[col.data];
            })
            remapped.push(a);
        });
        alert(JSON.stringify(remapped));
    }

  });


  $('a.toggle-vis').on( 'click', function (e) {
    e.preventDefault();

    // Get the column API object
    var column = table.column( $(this).attr('data-column') );

    // Toggle the visibility
    column.visible( ! column.visible() );
  } );

  //table.buttons().container().appendTo( $('.botoes', table.table().container() ) );

  // Setup - add a text input to each footer cell

  $('.OCS  tfoot th').each( function (i) {
      var title = $('tfoot th').eq( $(this).index() ).text();
      $(this).html( '<input type="text" placeholder="Search '+title+'" data-index="'+i+'"/>' );

  } );

  // Filter event handler

  $( table.table().container() ).on( 'keyup', 'tfoot input', function () {
      table
          .column( $(this).data('index') )
          .search( this.value )
          .draw();
  } );


} );



My HTML file (data generated by PHP with sql server ):



<!DOCTYPE html> <head> <link rel="stylesheet" type="text/css" href="/Tools/datatables.css"/> <script type="text/javascript" src="/Tools/datatables.js"></script> <script type="text/javascript" src="scripts/datatables_config.js"></script> </head> <body> <div name='dados' id='' class='container'> <button id='enviar' name='enviar' class='btn btn-primary'> Enviar </button> <table id='example' class='display table table-bordered table-striped OCS' cellspacing='0' width='100%'> <thead> <tr> <th>OC</th> <th>O.C.</th> <th>Fornecedor</th> <th>Itens</th> <th>Valor Total</th> <th>Emiss&atilde;o</th> <th>Email</th> </tr> </thead> <tfoot> <tr> <th>OC</th> <th>O.C.</th> <th>Fornecedor</th> <th>Itens</th> <th>Valor Total</th> <th>Emiss&atilde;o</th> <th>Email</th> </tr> </tfoot> <tbody> <tr> <td> 038381 </td> <td> Link para relatorio de oc </td> <td> Nome do Fornecedor </td> <td> 1 </td> <td> 1520.4 </td> <td> 07/06/2017 </td> <td> email@email.com </td> </tr> <tr> <td> 038381 </td> <td> Link para relatorio de oc </td> <td> Nome do Fornecedor </td> <td> 1 </td> <td> 1520.4 </td> <td> 07/06/2017 </td> <td> email@email.com </td> </tr> </tbody> </table> </div> </body> </html>

Any ideas?

Answers

This discussion has been closed.