1

I'm trying to add a custom html header to my datatable, in particular I want change the search and lenght position to the left, and add the custom html to the right, I try this code:

$(document).ready(function() {
     var table = $('#example').DataTable( {
          "dom": 'l<"toolbar">frtip'
    } );

    $("div.toolbar").html('<div class="float-right">'+
                        '<form class="form-inline">' +
                            '<div class= "form-group" > ' +
                                '<div class="input-group">' +
                                    '<input type="text" class="form-control form-control-light" id="dash-daterange">' +
                                    '<div class="input-group-append">' +
                                        '<span class="input-group-text bg-primary border-primary text-white">' +
                                            '<i class="mdi mdi-calendar-range font-13"></i>' +
                                        '</span>' +
                                    '</div >' +
                                '</div >' +
                            '</div >' +
                            '<a class="btn btn-primary ml-2">' +
                                '<i class="mdi mdi-filter-variant"></i>' +
                            '</a>'+
                        '</form>' +
                    '</div>');

} );

I add a JSFiddle here.

What I did wrong?

UPDATE

Expected result:

Show [10 v] entries             calendar[]  filter[]
Search...

2 Answers 2

1

You can divide the section before <table> in two equal sized boxes :

.dt-box {
  float: left;
  width: 50%;
}
dom: '<"dt-box"lf><"#toolbar.dt-box">rtip'

The headache is the heavily styled elements, you must change a few defaults:

.dataTables_wrapper .dataTables_filter {
  clear: both;
  float: left !important;
  margin-top: 5px;
}
.dataTables_wrapper .dataTables_length {
  text-align: left !important;
  float: left;
}

Now you can add your own form by

$('#toolbar').html('....')

forked fiddle -> https://jsfiddle.net/xu4fdpk9/

NB: Additional styling is needed if the above should be responsive or for example fit into a very narrow table.


I really cannot reproduce what you describe (or I may misunderstand) but you could wrap the .dt-box'es into a flex container:

.dt-flex {
  display: flex;
}
dom: '<"dt-flex"<"dt-box"lf><"#toolbar.dt-box">>rtip'

Dont know if that make any difference, but both left and right dt-box is by that guaranteed to have same height.

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

1 Comment

Exactly what I'm looking for, just a thing: when I reduce the window dimension the button of the form overlay the datatable, how can I prevent this? In the fiddle the button is not displayed because the icon pack is missing, you can try replacing the a tag with a button
0

I am not sure I understood. You want the Search field and Show entries to be in the left side and some custom html, which I assume is that nameless box, in the right side?

I would suggest adding a bit of styling for that, something like (updated):

(<style type='text/css'>)

#example_wrapper {
  padding-top: 50px;
}

#example_filter {
  position: absolute;
  left: 0px;
  top: 30px;
}

#example_length 
{
  position: absolute;
  left: 0px;
  top: 0px;
}

.toolbar {
  position: absolute;
  top: 0px;
  right: 0px;
}

(</style>)

2 Comments

Thanks for the reply Jan, I mean: I'm trying to align the search bar and the length vertically to the left, and to the right should be displayed my form
Well, the layout is using relatively positioning which means you can fix these items at absolute positions. Like this?

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.