Keep DataTables Controls on One Row

The page-length selector and search field can appear on separate rows when the DataTables layout uses fixed-width Bootstrap columns. Use a flexible toolbar row instead. This procedure shows a DataTables 1.x setup and the current DataTables 2 alternative.

Before you start

Check your installed DataTables version. Use dom with DataTables 1.x. Use layout with DataTables 2 or later.

Load the DataTables Bootstrap integration after Bootstrap. Load your custom CSS after both libraries.

DataTables 1.x procedure

1. Place both controls in one wrapper

Use l for the page-length selector and f for the search field. Do not assign Bootstrap column classes to these controls.

$('#recordsTable').DataTable({
    dom: '<"dt-top-controls"lf>rtip',
    pageLength: 25,
    lengthMenu: [10, 25, 50, 100]
});

The dom value places both controls inside one wrapper. The remaining letters add the processing display, table, record count, and page links.

2. Add flexible toolbar styles

Add this CSS after the DataTables Bootstrap stylesheet:

.dataTables_wrapper .dt-top-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem 1rem;
    margin-bottom: 0.5rem;
}

.dataTables_wrapper .dt-top-controls .dataTables_length,
.dataTables_wrapper .dt-top-controls .dataTables_filter {
    flex: 0 1 auto;
    min-width: 0;
    margin: 0;
}

.dataTables_wrapper .dt-top-controls .dataTables_filter {
    margin-left: auto;
    text-align: right;
}

@media (max-width: 575.98px) {
    .dataTables_wrapper .dt-top-controls .dataTables_filter {
        margin-left: 0;
        text-align: left;
    }
}

At wider screen sizes, the selector stays at the start of the row. The search field stays at the end. At narrow screen sizes, the controls can wrap to prevent overlap.

3. Remove conflicting column rules

Remove markup such as <"col-md-6"l> and <"col-md-6"f> from this toolbar. Bootstrap column widths plus flex gaps can exceed the row width. The browser can then move a control to another line.

Also remove older CSS rules that set fixed widths or force each control to width: 100%.

4. Check the result

  1. Clear cached CSS and JavaScript, then reload the page.
  2. Check the table at desktop width. Confirm both controls share one row.
  3. Check the table at mobile width. Confirm both controls remain readable and usable.

DataTables 2 or later

DataTables 2 replaces the deprecated dom option with layout. Put the selector and search field in the top layout row:

new DataTable('#recordsTable', {
    layout: {
        topStart: 'pageLength',
        topEnd: {
            search: {
                placeholder: 'Search records'
            }
        },
        bottomStart: 'info',
        bottomEnd: 'paging'
    },
    pageLength: 25,
    lengthMenu: [10, 25, 50, 100]
});

The Bootstrap integration places the page-length control at the start of the row and search at the end. Avoid custom width rules on the generated layout cells unless your design needs them.

Troubleshooting

SymptomCheck
Controls still appear on separate rowsInspect the wrapper and remove fixed-width column classes.
Search field pushes selector off screenCheck custom width, min-width, and margin rules.
Controls overlap on mobileKeep flex-wrap: wrap enabled.
CSS has no effectConfirm CSS loads after DataTables styles and targets the actual wrapper class.
DataTables 2 reports an obsolete optionReplace dom with layout.

Summary

  • Place the page-length selector and search field in one layout row.
  • Use a flexible wrapper instead of fixed Bootstrap columns.
  • Allow controls to wrap on narrow screens.
  • Use layout with DataTables 2 or later.

References

Leave a Reply

Your email address will not be published. Required fields are marked *