Datatable styling so bootstrap button appears on same row as other elements
Asked Answered
P

8

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' );
      }
    }]
  });
});
Psychophysiology answered 27/8, 2015 at 14:48 Comment(1)
CSS white-space: nowrap;?Wolfe
I
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.

Interface answered 27/8, 2015 at 15:19 Comment(10)
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?Psychophysiology
@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.Interface
Awesome.. How do you add a second custom button to that same line?Psychophysiology
@ToddT, I'm still learning the new features in 1.10.8, but I assume it should be done as in this jsFiddle.Interface
Once again.. thanks so much! I tried that but missed the comma between the two buttons..Psychophysiology
+1 @Interface 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 datatables.net/reference/option/domCleopatra
Also, because I wanted to use icon buttons I used a slightly different approach. So anybody who is interested see this fiddle... jsfiddle.net/vckbwjqb/2Cleopatra
@Will.Harris, added more details with proper way to use direct insertion.Interface
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: 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: datatables.net/reference/option/buttons.buttonsHenleigh
@ToddT, Your example on the jsfiddle worked like a charm for me!! Awesome. Many thanks!!Fireplug
S
9

In my case I just added these styles:

.dataTables_length,.dataTables_filter {
    margin-left: 10px;
    float: right;
}
Squiggle answered 11/9, 2019 at 9:12 Comment(0)
C
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

Concertina answered 17/9, 2015 at 9:16 Comment(0)
A
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; 
 }
Armagh answered 30/8, 2020 at 18:57 Comment(0)
S
0

In My case, I just added this CSS

.dataTables_wrapper .dataTables_length {
    float:left;
    position: absolute;
}
.dataTables_wrapper .dataTables_filter {
    float: right;
}
Stagy answered 11/6, 2021 at 14:13 Comment(0)
M
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; }

Mohock answered 17/4, 2022 at 9:14 Comment(0)
P
-3

Have a look at documentation of DataTable Here.

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

Pyrostat answered 27/8, 2015 at 14:58 Comment(3)
Yea even that example stacks the search bar and the text they inserted.. see here live.datatables.net/coqisori/1Psychophysiology
.toolbar { float: left; } add this. it ill work like a charm.Pyrostat
That will not work correctly with Bootstrap styling. Besides you missed the B option.Interface
R
-3

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

Retard answered 27/8, 2015 at 15:3 Comment(2)
Sounds good, but how do you attach a class to both a custom button and the items in the header of the Datatables plugin??Psychophysiology
Why can't you? Post your code so I can see what you mean.Retard

© 2022 - 2024 — McMap. All rights reserved.