Adding buttons after load.

Adding buttons after load.

MickBMickB Posts: 103Questions: 25Answers: 2

Hey,

Should I be able to do something like this?:

let table = $('#datatables-table');

    table.button().add( 0, {
        action: function ( e, dt, button, config ) {
            dt.ajax.reload();
        },
        text: 'Reload table'
    } );

I'm just trying to add these standard buttons after the initialise:

buttons: [{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor}],

Mick

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Hi Mick,

    In the above table is a jQuery instance, not a DataTables API instance. You would need to use:

    let table = $('#datatables-table').DataTable();
    

    to get a DataTables API instance (which has the button() method available on it).

    With the above I think it should probably be giving a Javascript error about the button function not existing.

    Regards,
    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Yes, that works now. I thought I did that initially and got an error because the table was already initialised. Thanks.

    What about adding the standard buttons at this point?

    buttons: [{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor}],
    

    FYI I am creating a Vue.js component for my Datatables and I am trying to pass

    [{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor}]
    

    As a variable but can't find a way to do it without it sticking " around it (when it's a string) and I can't pass it as JSON, because the editor needs to be without quotes.

    This is why I am adding the buttons after the load, I will use this component whenever I use a Datatable and the buttons I require will vary.

    Mick

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Hey Allan,

    Did you see this?

    "What about adding the standard buttons at this point?"

    Thanks,

    Mick

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Sorry I missed that part.

    You can use the button().add() method to add the Editor buttons as well:

    table.button().add( 0, {
      extend:'create',
      editor: editor
    } );
    

    Any button that can be added using the buttons.buttons array can also be added using the API at any time after the initialisation.

    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Brilliant!

    Thanks

This discussion has been closed.