3

I've searched and searched but just can't figure this out. I read in the documentation that you need to add initComplete to get the buttons to appear but I still have no luck.

Am I missing something? I've tried with and without dom, different ways of adding buttons, etc. I have no problem doing this without the ajax call, that works fine. There is something different here that I haven't figured out.

!doctype html>
<html lang="en" dir="ltr">
  <head>
    <title>Forensics Sample Database</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1000, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Oxygen:400,700">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/buttons.bootstrap.min.css">
    <link rel="stylesheet" href="./layout.css"> 
    <script charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script charset="utf-8" src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.js"></script>
    <script charset="utf-8" src="//cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/buttons.bootstrap.min.js"></script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"</script>
    <script charset="utf-8" src="//cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"</script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"</script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/buttons.colVis.min.js"</script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"</script>
    <script charset="utf-8" src="//cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"</script>
    <script charset="utf-8" src="//cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>
    <script charset="utf-8" src="webapp.js"></script>
  </head>

and in my webapp.js file

$(document).ready(function(){

  // On page load: datatable
  var tablesamples = $('#tablesamples').DataTable({
    "ajax": "data.php?job=get_samples",
    "columns": [
      { "data": "sample_id" },
      { "data": "storage_location" },
      { "data": "depositor" },
      { "data": "sample_name",        "sClass": "sample_name" },
      { "data": "date_received" },
      { "data": "supplier" },
      { "data": "source" },
      { "data": "concentration" },
      { "data": "volume" },
      { "data": "prep_exp" },
      { "data": "age" },
      { "data": "gender" },
      { "data": "ethnicity" },
      { "data": "draw_date" },
      { "data": "extraction_date" },
      { "data": "screened" },
      { "data": "collection_tube" },
      { "data": "matrix" },
      { "data": "notes" },
      {"data": "functions", "sClass": "functions"}     
    ],
   "dom": '<"top"i>rt<"bottom"flp><"clear">',
 // "dom": 'Blfrtp',
    "scrollY": 400,
    "scrollX": true,
    buttons: [ 'excel '],
      "initComplete": function(settings, json) {
    alert( 'DataTables has finished its initialisation.' );
  }


  /*  initComplete: function(){
          var api = this.api();

          new $.fn.dataTable.Buttons(api, {
             buttons: [
                {
                   'excel'
                   }
                }
             ]
            });
           api.buttons().container().appendTo( 'tablesamples' + api.table().container().id + ' .col-sm-6:eq(0)' ); 
           */
  });
 table.buttons().container()
   .appendTo( '#tablesamples_wrapper .col-sm-6:eq(0)');

    enter code here
4
  • If you could setup a JSFiddle with sample data I can take a crack at this. Commented Nov 21, 2016 at 20:15
  • Sorry I'm struggling to do that with the ajax. :/ I'll keep trying though. Thank you. Commented Nov 22, 2016 at 4:58
  • Instead of ajax just copy your array of objects and inline it as a variable. Then instead of the "ajax" property set the "data" property to this variable. That will give me a good example to work with. Commented Nov 22, 2016 at 6:09
  • Unfortunately, looks like it works on jsfiddle so I have no idea what is going on jsfiddle.net/ntcwust8/66 Commented Nov 23, 2016 at 6:03

2 Answers 2

3

So I'm not sure why this works but it does

$(document).ready(function(){
  // On page load: datatable
  var tablesamples = $('#tablesamples').DataTable({
   'ajax': 'data.php?job=get_samples',
   'dom': 'Bfrtip',
'buttons': [
    {
      text: 'Excel',
      extend: 'excel',
      exportOptions: {
        modifier: {
          page: 'current'
        }
      }
    }
],
'columns': [
  { 'data': 'sample_id' },
  { 'data': 'storage_location' }     
],
'oLanguage': {
  'oPaginate': {
    'sFirst':       ' ',
    'sPrevious':    ' ',
    'sNext':        ' ',
    'sLast':        ' ',
  },
  'sLengthMenu':    'Records per page: _MENU_',
  'sInfo':          'Total of _TOTAL_ records (showing _START_ to _END_)',
  'sInfoFiltered':  '(filtered from _MAX_ total records)'
},
'scrollX': true,  
initComplete: function () {
  var api = this.api();
    console.log( api.buttons().container()[0].nodeName );
}
});
Sign up to request clarification or add additional context in comments.

1 Comment

just noticed something in the code in your original post. You have "table.buttons().container() .appendTo( '#tablesamples_wrapper .col-sm-6:eq(0)');" outside of the document.ready(...). So it will execute before the table is initialized. It is possibly just a copy/paste error but figured I should point it out just in case
1

This Worked For Me try to Customize it for your need :

var table = $( document ).ready(function() {

        $('#clientTable').DataTable( {
            "processing": false,
            "dom": 'Bfrtip',
            "serverSide": false,
            "bSortable": true,
            "lengthChange": true,
            "responsive": true,

            "ajax": {
                    "url": "../resources/models/jsonListClient.php",
                    "type": "POST",
                    "dataSrc": ""
            },
            "columns": [
                    { "data": "nom" },
                    { "data": "ville" },
                    { "data": "ice" },
                    { "data": "adresse" },
            ],
            "language": {
            "sProcessing": "Traitement en cours...",
            "sSearch": "Rechercher&nbsp;:",
                        "sLengthMenu":     "Afficher _MENU_ &eacute;l&eacute;ments",
            "sInfo":           "Affichage de l'&eacute;l&eacute;ment _START_ &agrave; _END_ sur _TOTAL_ &eacute;l&eacute;ments",
            "sInfoEmpty":      "Affichage de l'&eacute;l&eacute;ment 0 &agrave; 0 sur 0 &eacute;l&eacute;ment",
            "sInfoFiltered":   "(filtr&eacute; de _MAX_ &eacute;l&eacute;ments au total)",
            "sInfoPostFix":    "",
            "sLoadingRecords": "Chargement en cours...",
            "sZeroRecords":    "Aucun &eacute;l&eacute;ment &agrave; afficher",
            "sEmptyTable":     "Aucune donn&eacute;e disponible dans le tableau",
                        "oPaginate": {
                "sFirst":      "Premier",
                "sPrevious":   "Pr&eacute;c&eacute;dent",
                "sNext":       "Suivant",
                "sLast":       "Dernier"
            },
            "oAria": {
                "sSortAscending":  ": activer pour trier la colonne par ordre croissant",
                "sSortDescending": ": activer pour trier la colonne par ordre d&eacute;croissant"
            }
            },

            buttons: [
                {
                        extend: 'print',
                        message: 'Imprimer',
                        text: 'Imprimer',
                },
                {
                        extend: 'copy',
                        message: 'Copier / Coller',
                        text: 'Copier',
                },
                {
                        extend: 'excelHtml5',
                        title: 'Export Liste'
                },
                {
                        extend: 'pdfHtml5',
                        title: 'Export Liste',
                }
                ,   {
                        extend: 'colvis',
                        text: 'Colonnes'
                } 
            ]

        });


    });

1 Comment

Adding the "dom": 'Bfrtip', worked for me. I was missing that. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.