Add buttons not work

Add buttons not work

Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

Hi,
I am trying to add buttons to a series of tables, but it does not work:

var table = $('table.table').DataTable({
  order: [[ 0, "desc" ]],
  "paging": false,
  "info": false,
  responsive: true,
  columnDefs:[
    {className: "text-right","targets":[3,4]},
    {className: "text-center","targets":[2,5]}
  ],
  "language":
    {
      "url": "<?php echo base_url('vendors/datatables.net/i18n/Spanish.json') ?>",
    }
});

<?php foreach ($lista as $key => $value): ?>
console.log(table.table(parseInt('<?php echo $key ?>')).data());
  table.table(parseInt(parseInt('<?php echo $key ?>'))).button().add(0, {
    text: 'Boton'
  });
<?php endforeach ?>

According to the referral, this should work.
On line 17 you can see that you get the data correctly.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    Did you load the buttons extension JS and CSS includes:
    https://datatables.net/extensions/buttons/

    You might need to use the dom option to place the buttons:
    https://datatables.net/extensions/buttons/#dom-parameter

    Kevin

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Ok, the problem here:

    var table = $('table.table').DataTable({
      order: [[ 0, "desc" ]],
      "paging": false,
      "info": false,
      responsive: true,
      columnDefs:[
        {className: "text-right","targets":[3,4]},
        {className: "text-center","targets":[2,5]}
      ],
      // "language":
      //   {
      //     "url": "<?php echo base_url('vendors/datatables.net/i18n/Spanish.json') ?>",
      //   }
    });
    

    In languaje, I do not know why but when I use languaje it does not display the button, any idea?

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    I don't actually see where you are adding the buttons, but if you use language.url you should use initComplete to add the buttons, since language.url makes the loading async.

    Allan

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    The buttons add in

    var table = $('table.table').DataTable({
      order: [[ 0, "desc" ]],
      "paging": false,
      "info": false,
      responsive: true,
      columnDefs:[
        {className: "text-right","targets":[3,4]},
        {className: "text-center","targets":[2,5]}
      ],
      "language":
        {
          "sSearch":         "Buscar:",
        }
    });
    
    <?php foreach ($lista as $key => $value): ?>
    var i = parseInt('<?php echo $key ?>');
    var wtable = table.table(i);
        new $.fn.dataTable.Buttons( wtable, {
            buttons: [
                {
                    text: '<span class="wbut" id="'+i+'_'+'<?php echo $value['id'] ?>">Agregar</span>',
                }
            ]
        } );
        wtable.buttons( 0, null ).container().prependTo(
            wtable.table().container()
        );
    <?php endforeach ?>
    

    In this way it works

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Answer ✓

    You'd need to add them in initComplete as I mentioned above. The language.url option makes the loading of the table async.

    Allan

  • Wilmer30Wilmer30 Posts: 33Questions: 10Answers: 1

    Thank you clarification

This discussion has been closed.