9

I am trying to add a button to my toolbar of my datatable. So, my datatable is:

var dataTable =  $('#employee-grid').DataTable(
{
    processing: true,
    serverSide: true,
    ajax: "employee-grid-data.php", // json datasource for AJAX Data

    "pagingType": "full_numbers",   //Adding Last and First in Pagination
    stateSave: true,
    "language":{                    //Custom Message Setting
                    "lengthMenu": "Display _MENU_ records per page",    //Customizing menu Text
                    "zeroRecords": "Nothing found - sorry",             //Customizing zero record text - filtered
                    "info": "Showing page _PAGE_ of _PAGES_",           //Customizing showing record no
                    "infoEmpty": "No records available",                //Customizing zero record message - base
                    "infoFiltered": "(filtered from _MAX_ total records)"   //Customizing filtered message
                },
    "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],        //For customizing number of data sets per page

});

And what I have done is like this:

    $(document).ready(function()
    {
        var dataTable =  $('#employee-grid').DataTable(
        {
            processing: true,
            serverSide: true,
            ajax: "employee-grid-data.php", // json datasource for AJAX Data

            "pagingType": "full_numbers",   //Adding Last and First in Pagination
            stateSave: true,
            "language":{                    //Custom Message Setting
                            "lengthMenu": "Display _MENU_ records per page",    //Customizing menu Text
                            "zeroRecords": "Nothing found - sorry",             //Customizing zero record text - filtered
                            "info": "Showing page _PAGE_ of _PAGES_",           //Customizing showing record no
                            "infoEmpty": "No records available",                //Customizing zero record message - base
                            "infoFiltered": "(filtered from _MAX_ total records)"   //Customizing filtered message
                        },
            "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],        //For customizing number of data sets per page
            "dom": '<"toolbar">frtip'
        });

        $("div.toolbar").html('<button type="button" id="any_button">Click Me!</button>');
    } );

But I am finding something like this:

enter image description here

But I like to have something like this-

enter image description here

Can anyone please help?

1
  • 1
    Could you edit your question to point out what's different about the two screenshots? Commented Sep 13, 2015 at 13:11

3 Answers 3

33

SOLUTION

Use the code below:

JavaScript:

var table = $('#example').DataTable({
   // ... skipped ...
   dom: 'l<"toolbar">frtip',
   initComplete: function(){
      $("div.toolbar")
         .html('<button type="button" id="any_button">Click Me!</button>');           
   }       
});   

CSS:

.toolbar {
    float:left;
}

DEMO

See this jsFiddle for code and demonstration.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank u very much friend :)
I'm surprised that I couldn't find the 'initComplete' event in the documentation though
Button appears but isn't clickable, had to modify the CSS to change position and z-index
If you using ajax this button will show after the data load. So instead of initComplete use preDrawCallback
1

You can also use datatable button.js. Here is the source link:

https://datatables.net/extensions/buttons/examples/initialisation/custom.html

Don't forget to add below libraries (as mentioned in the above URL)

https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js

Comments

0

alternatively you can do also like below:

$('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                text: 'My button',
                action: function ( e, dt, node, config ) {
                    alert( 'Button activated' );
                }
            }
        ]
    } );

Source :https://datatables.net/extensions/buttons/examples/initialisation/custom.html

Comments

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.