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

后端 未结 6 2061
既然无缘
既然无缘 2021-01-30 01:41

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 inste

相关标签:
6条回答
  • 2021-01-30 02:11

    Have a look at documentation of DataTable Here.

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

    0 讨论(0)
  • 2021-01-30 02:17

    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.

    0 讨论(0)
  • 2021-01-30 02:20

    In my case I just added these styles:

    .dataTables_length,.dataTables_filter {
        margin-left: 10px;
        float: right;
    }
    
    0 讨论(0)
  • 2021-01-30 02:25

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

    0 讨论(0)
  • 2021-01-30 02:28

    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 讨论(0)
  • 2021-01-30 02:29

    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; 
     }
    
    0 讨论(0)
提交回复
热议问题