how can I add select, checkbox, radio buttons, and input filters on datatable columns????
hi there,
I want to add select, checkbox, radio buttons, and input filters on datatable columns, please let me know how I can achieve this.
I want to add
- radio filter on the "gender" column.
- checkbox filter on the "user type" column (admin, editor, writer, reviewer, etc)
- select filter on the "status" column (predefined statuses (published, draft, private, etc)
- input filter on the "age" column (please also share how I can add the "number" input filed and "text" input field.
Thanks, waiting for your reply.
The examples are very helpful. While testing DataTables with API responses, I usually validate JSON before loading it into the table. I use the JSON Formatter available on ProKalkulator, which helps detect formatting errors quickly and makes debugging easier. ProKalkulator
Yes, you can add select dropdowns, checkboxes, radio buttons, and text input filters to DataTable columns by placing the controls in the table header or footer and connecting them to the DataTables search API. This allows users to filter each column individually and makes large tables much easier to navigate. If you enjoy personalizing text, زخرفة اسماء helps you generate stylish and decorative Arabic names using unique symbols and Unicode characters, making them perfect for social media profiles, gaming usernames, and creative designs.```$('#myTable thead th').each(function () { var title = $(this).text(); $(this).html(''); });
var table = $('#myTable').DataTable();
table.columns().every(function () { var that = this;
$('input', this.header()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});... ```
Very useful guide. The combination of radio buttons, checkboxes, select filters, and numeric inputs covers most real-world DataTable use cases. Besides testing filter logic, I also check how applications handle different Unicode characters in text fields. Font Style
is a handy resource for generating stylish text and special characters that can be used to test input validation and character rendering in web applications.
Thanks for sharing such a detailed solution. I really liked how you explained the use of column().search() with radio buttons, checkboxes, select menus, and different input fields. The examples make it much easier to understand how custom column filters can be implemented without relying on extra plugins. I also agree that when serverSide: true is enabled, the backend should process the filters correctly to maintain good performance. Before deploying, I always test different input types, including numbers, text, and Unicode characters, to make sure everything works as expected across browsers. While testing Unicode text rendering, I found YaziStil useful because it provides different Unicode text styles and special characters that help verify how input fields and DataTables handle custom character rendering. Overall, this is a very helpful reference for anyone implementing advanced DataTable filtering.
Yes, this can be implemented with the DataTables column().search() API. No additional Metronic filtering plugin is required.
Add a second header row for the filters:
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>User Type</th>
<th>Status</th>
<th>Age</th>
</tr>
<tr class="filters">
<th></th>
<th ></th>
<th ></th>
<th ></th>
<th ></th>
</tr>
</thead>
Then initialize each column filter inside initComplete:
$('#kt_datatable').DataTable({
orderCellsTop: true,
initComplete: function () {
const api = this.api();
const escapeRegex = $.fn.dataTable.util.escapeRegex;
// Adjust these indexes to match your table.
const genderColumn = api.column(1);
const typeColumn = api.column(2);
const statusColumn = api.column(3);
const ageColumn = api.column(4);
const exactSearch = value =>
value ? '^' + escapeRegex(value) + '$' : '';
// Gender: radio buttons
$('#gender-filter')
.html(`
<label><input type="radio" name="gender" value="" checked> All</label>
<label><input type="radio" name="gender" value="Male"> Male</label>
<label><input type="radio" name="gender" value="Female"> Female</label>
`)
.on('change', 'input[name="gender"]', function () {
genderColumn
.search(exactSearch(this.value), true, false)
.draw();
});
// User type: multiple checkboxes
$('#type-filter')
.html(`
<label><input type="checkbox" value="Admin"> Admin</label>
<label><input type="checkbox" value="Editor"> Editor</label>
<label><input type="checkbox" value="Writer"> Writer</label>
<label><input type="checkbox" value="Reviewer"> Reviewer</label>
`)
.on('change', 'input[type="checkbox"]', function () {
const values = $('#type-filter input:checked')
.map(function () {
return escapeRegex(this.value);
})
.get();
const regex = values.length
? '^(?:' + values.join('|') + ')$'
: '';
typeColumn.search(regex, true, false).draw();
});
// Status: select
$('#status-filter')
.html(`
<select class="form-select form-select-sm">
<option value="">All statuses</option>
<option value="Published">Published</option>
<option value="Draft">Draft</option>
<option value="Private">Private</option>
</select>
`)
.on('change', 'select', function () {
statusColumn
.search(exactSearch(this.value), true, false)
.draw();
});
// Age: numeric input
$('#age-filter')
.html(`
<input
type="number"
min="0"
class="form-control form-control-sm"
placeholder="Age"
>
`)
.on('keyup change clear', 'input', function () {
ageColumn
.search(exactSearch(this.value), true, false)
.draw();
});
// Prevent filter controls from triggering column sorting.
$('.filters').on(
'click',
'input, select, label',
function (event) {
event.stopPropagation();
}
);
}
});
For a normal text input, use:
column.search(this.value).draw();
The checkbox filter combines selected values with an OR expression, while radio, select and numeric filters use exact matching.
You can also refer to the current official examples:
If serverSide: true is enabled, the Laravel/Yajra backend must handle these filters as well. For multiple checkbox values, sending an array and processing it with whereIn() or filterColumn() is usually preferable.
To add custom select, checkbox, radio, and numeric input filters to Datatables, you need to use the initComplete API callback to target the column headers and append your custom HTML controls.
Here is how you handle the specific column input for the age column using custom JS column filtering:
JavaScript $('#kt_datatable').DataTable({ initComplete: function () { // Target the Age Column (e.g., column index 3) this.api().column(3).every(function () { var column = this; var input = $('') .appendTo($(column.header())) .on('keyup change clear', function () { if (column.search() !== this.value) { column.search(this.value).draw(); } }); }); } }); Testing UI Inputs: When implementing custom input fields or validating that dynamic form scripts handle pure numeric values properly without layout shifting, you can see now how a clean, lightweight JS utility processes input data without loading heavy framework dependencies. Testing your Datatable layout against simple input forms ensures your custom CSS wrappers render cleanly across mobile viewports.
Hi,
At the moment we do not have such an example but we will definitely consider adding it in a future update. In the meantime you can refer to the Datatable API examples on how to add inputs in data table columns.
Regards.