34

I'm using jQuery DataTables plugin and Bootstrap on my rails site. I can't get my custom button and other table header elements to nest in the same row, They are stacked instead of being inline.

Any suggestions to get them all on the same line?

Here is some of the JavaScript I've used:

$(document).ready(function() {
  $('#products').DataTable( {
    dom: 'Blfrtip',
    buttons: [ {
      text: 'Pull my products',
      action: function ( e, dt, node, config ) {
        alert( 'Button activated' );
      }
    }]
  });
});
Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
ToddT
  • 3,084
  • 4
  • 39
  • 83

8 Answers8

78

SOLUTION #1

This is the most confusing part with using Bootstrap style for jQuery DataTables and it's undocumented so far. Bootstrap extension overrides default dom which can be confirmed by viewing its source code.

You have to use specially crafted dom option similar to shown below:

dom: 
    "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>" +
    "<'row'<'col-sm-12'tr>>" +
    "<'row'<'col-sm-5'i><'col-sm-7'p>>",

You can be as creative as you want by using Bootstrap row and col-* classes in the dom option.

See this jsFiddle for code and demonstration.

SOLUTION #2

You can also use direct insertion method as shown in this example because default dom option used for Bootstrap styling is quite complex.

var table = $('#example').DataTable({
   initComplete: function(){
      var api = this.api();

      new $.fn.dataTable.Buttons(api, {
         buttons: [
            {
               text: 'Pull my products',
               action: function ( e, dt, node, config ) {
                  alert( 'Button activated' );
               }
            }
         ]
      });

      api.buttons().container().appendTo( '#' + api.table().container().id + ' .col-sm-6:eq(0)' );  
   }
});

Note that code differs from the example referenced above because there is an issue with DataTables 1.10.9 preventing direct insertion of buttons if there is no B character in dom option or dom option is not specified.

See this jsFiddle for code and demonstration.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • 3
    Wow.. just wow.. Thanks Gyrocode.. totally makes sense, but how in the world did you figure this one out?? One last question.. How do you add in a second custom button on that same line? – ToddT Aug 27 '15 at 15:26
  • 2
    @ToddT, had the same problem and figured it out by inspecting the source code of the extension. jQuery DataTables is an excellent library but I wish this part was documented better. – Gyrocode.com Aug 27 '15 at 15:27
  • Awesome.. How do you add a second custom button to that same line? – ToddT Aug 27 '15 at 15:28
  • @ToddT, I'm still learning the new features in 1.10.8, but I assume it should be done as in [this jsFiddle](https://jsfiddle.net/vckbwjqb/1/). – Gyrocode.com Aug 27 '15 at 15:31
  • 1
    Once again.. thanks so much! I tried that but missed the comma between the two buttons.. – ToddT Aug 27 '15 at 15:35
  • +1 @Gyrocode.com great stuff, been looking for something like this for a while! Not sure if the docs always displayed this info but it also appears to be in there now under the styling section https://datatables.net/reference/option/dom – Will.Harris Nov 02 '15 at 14:38
  • Also, because I wanted to use icon buttons I used a slightly different approach. So anybody who is interested see this fiddle... https://jsfiddle.net/vckbwjqb/2/ – Will.Harris Nov 02 '15 at 14:40
  • @Will.Harris, added more details with proper way to use direct insertion. – Gyrocode.com Nov 02 '15 at 15:11
  • Thank you very much! This pointed me in the right direction. BTW, if anyone tried this on version 1.10.15 and didn't get it working, then you need to install the Buttons extension which you can find here: https://datatables.net/download/release and, furthermore, to get the buttons to look more Bootstrap-3-like, you need to use the `className` option within the button definition like so: `className: 'btn btn-primary'`. Also, here's the documentation page for buttons in general: https://datatables.net/reference/option/buttons.buttons – racl101 Oct 03 '17 at 21:49
  • @ToddT, Your example on the jsfiddle worked like a charm for me!! Awesome. Many thanks!! – sunitkatkar Dec 18 '18 at 23:03
9

In my case I just added these styles:

.dataTables_length,.dataTables_filter {
    margin-left: 10px;
    float: right;
}
Dima L.
  • 3,443
  • 33
  • 30
1

You have to put two divs: one for the datatable buttons and other for your custom code

       var table = $("#tbl_oferta").dataTable({
                dom: '<"pime-grid-button"B><"pime-grid-filter">frtip',
        ...
       });

       $("div.pime-grid-filter").html('<b>Custom tool bar! Text/images etc.</b>');

Then define div class in your css:

.pime-grid-button{float:left}
.pime-grid-filter{float:left;margin-left:10px}        

Sample Image

0

This is how I did it.

"dom": '<"row"<"col-6"<"d-flex justify-content-start"<""l><"ml-4"i>>><"col-6"<"d-flex justify-content-end"<""f>>>>tp'

Also add some custom CSS to make things appear more inline.

div.dataTables_wrapper div.dataTables_info{
   padding-top: 0.2em !important; 
 }
JamesH
  • 11
  • 3
0

In My case, I just added this CSS

.dataTables_wrapper .dataTables_length {
    float:left;
    position: absolute;
}
.dataTables_wrapper .dataTables_filter {
    float: right;
}
Nazim
  • 1
0

You can add buttons in toolbar menu like shown below:

        initComplete: function(){
            $("div.toolbar").html('<input type="button");          
        }

then add this toolbar to DOM element of Datatables as shown below:

    dom: "<'row'<'col-sm-2'l><'toolbar'><'col-sm-8'f>>" +
                     "<'row'<'col-sm-12'tr>>" +
                     "<'row'<'col-sm-5'i><'col-sm-7'p>>",
                

One can align this toolbar using css like below: .toolbar { float: left; margin-left: 60px; }

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
poonam
  • 1
  • 1
-3

Have a look at documentation of DataTable Here.

try this dom: '<"toolbar">frtip'

Asad Ali
  • 640
  • 6
  • 18
-3

Put style="display: inline" on the elements you want to display inline.

raduation
  • 792
  • 5
  • 11